【发布时间】:2014-09-26 00:38:45
【问题描述】:
我这里有这段代码,可以解析来自网络的 som 信息:
import lxml.html
from lxml.etree import XPath
import json
url = "http://gbgfotboll.se/information/?scr=table&ftid=51168"
date = '2014-09-27'
# use this in real mode: currentDate = (time.strftime("%Y-%m-%d"))
list = []
id = 0
score = ""
rows_xpath = XPath("//*[@id='content-primary']/table[3]/tbody/tr[td[1]/span/span//text()='%s']" % (date))
time_xpath = XPath("td[1]/span/span//text()[2]")
team_xpath = XPath("td[2]/a/text()")
html = lxml.html.parse(url)
for row in rows_xpath(html):
time = time_xpath(row)[0].strip()
team = team_xpath(row)[0]
list.append("%d:"%id + time + " " + team + " " + score)
id += 1
print json.dumps(list)
哪个打印:
0:13:00 Romelanda UF - IK Virgo (empty score for now)
1:15:00 Kode IF - IK Kongah\xe4lla (empty score)
etc..
我的第一个子问题是,一些已解析的数据将包含字母“唓䔓ö”我将如何修复以便打印出正确的字母,如您在打印的结果(第二行)中看到的那样out "Kongah\xe4lla" 应该是 "Konghälla"
主要问题我如何将该列表转换为字典,以便最终的 json 输出如下:
{"id":"0", "time":"13:00", "teams":"Romelanda UF - IK Virgo", "score":"empty" }
etc...
谢谢!!!
【问题讨论】:
标签: python json parsing dictionary