som*_*off 24 python parsing lxml
我正在使用lxml.html编写一个脚本来解析网页.我在我的时间里做了很多BeautifulSoup,但由于它的速度,我现在正在尝试lxml.
我想知道库中最明智的方法是做相当于Javascript的InnerHtml - 即检索或设置标签的完整内容.
<body>
<h1>A title</h1>
<p>Some text</p>
</body>
因此InnerHtml是:
<h1>A title</h1>
<p>Some text</p>
我可以使用黑客(转换为字符串/正则表达式等)来做到这一点,但我假设有一个正确的方法来使用由于不熟悉我缺少的库.谢谢你的帮助.
编辑:感谢pobk如此快速有效地向我展示了这方面的方法.对于任何尝试相同的人,这是我最终得到的:
from lxml import html
from cStringIO import StringIO
t = html.parse(StringIO(
"""<body>
<h1>A title</h1>
<p>Some text</p>
Untagged text
<p>
Unclosed p tag
</body>"""))
root = t.getroot()
body = root.body
print (element.text or '') + ''.join([html.tostring(child) for child in body.iterdescendants()])
请注意,lxml.html解析器将修复未关闭的标记,因此请注意这是否存在问题.
lor*_*mus 15
很抱歉再次提出这个问题,但我一直在寻找解决方案,而你的问题包含一个错误:
<body>This text is ignored
<h1>Title</h1><p>Some text</p></body>
直接位于根元素下的文本将被忽略.我最终这样做了:
(body.text or '') +\
''.join([html.tostring(child) for child in body.iterchildren()])
pob*_*obk 10
您可以使用根节点的getchildren()或iterdescendants()方法获取ElementTree节点的子节点:
>>> from lxml import etree
>>> from cStringIO import StringIO
>>> t = etree.parse(StringIO("""<body>
... <h1>A title</h1>
... <p>Some text</p>
... </body>"""))
>>> root = t.getroot()
>>> for child in root.iterdescendants(),:
...  print etree.tostring(child)
...
<h1>A title</h1>
<p>Some text</p>
这可以简化如下:
print ''.join([etree.tostring(child) for child in root.iterdescendants()])
| 归档时间: | 
 | 
| 查看次数: | 12661 次 | 
| 最近记录: |