我试图用Python中的lxml解析HTML页面。在
在HTML中有以下结构:
Title
Some text with other tags.
More text.
More text[2].
Title[2]
Description.
Title[3]
Description[1].
Description[2].
***
and so on...
***
我需要将此HTML解析为以下JSON:
^{pr2}$
我可以读取所有带有标题的h5标记,并使用以下代码将它们写入JSON:array = []
for title in tree.xpath('//h5/text()'):
data = {
"title" : title,
"text" : ""
}
array.append(data)
with io.open('data.json', 'w', encoding='utf8') as outfile:
str_ = json.dumps(array,
indent=4, sort_keys=True,
separators=(',', ' : '), ensure_ascii=False)
outfile.write(to_unicode(str_))
问题是,我不知道如何读取