【发布时间】:2011-10-01 11:26:26
【问题描述】:
我想使用 Python 从this 网站中提取 one-iner-texts。 HTML 中的消息如下所示:
<div class="olh_message">
<p>foobarbaz <img src="/static/emoticons/support-our-fruits.gif" title=":necta:" /></p>
</div>
到目前为止,我的代码如下所示:
import lxml.html
url = "http://www.scenemusic.net/demovibes/oneliner/"
xpath = "//div[@class='olh_message']/p"
tree = lxml.html.parse(url)
texts = tree.xpath(xpath)
texts = [text.text_content() for text in texts]
print(texts)
然而,现在我只得到foobarbaz,但我也想在其中得到img的标题参数,所以在这个例子中foobarbaz :necta:。看来我需要 lxml 的 DOM 解析器来做到这一点,但我不知道怎么做。谁能给我一个提示?
提前致谢!
【问题讨论】:
标签: python dom xpath html-parsing lxml