Python-lxml /获取xpath的全部内容

仅以Twitter为例,此代码从Twitter页面上抓取了第五条推文.该页面包含一个链接,但是当我尝试使用lxml和xpath将其拉起时,该页面仅显示将链接结尾的文本.

脚本:

import urllib2
from lxml import etree

xpathselector = "/html/body/div/div[2]/div/div[5]/div[2]/div/ol/li[5]/div/div/p"
url =  "https://twitter.com/memphismayfire"
response = urllib2.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)
result = tree.xpath(xpathselector)

print result[0].text

印刷品:

‘Miles Away’ Acoustic is available on iTunes! Who’s downloaded the single?! Let’s get it up the Singles Chart!! Link:

来自xPath位置的HTML:

<p class="js-tweet-text tweet-text">'Miles Away' Acoustic is available on iTunes! Who's downloaded the single?! Let's get it up the Singles Chart!! Link: <a title="http://smarturl.it/mmf-miles-away" target="_blank" class="twitter-timeline-link" data-expanded-url="http://smarturl.it/mmf-miles-away" dir="ltr" rel="nofollow" href="http://t.co/fU2hVqAiSq" f52ae163cfcf0237f="true"><span class="tco-ellipsis"></span><span class="invisible">http://</span><span class="js-display-url">smarturl.it/mmf-miles-away</span><span class="invisible"></span><span class="tco-ellipsis"><span class="invisible">&nbsp;</span></span></a><div ida2bb72480="_p_mzkte2cwofawsu3r.t.co" style="cursor: pointer; width: 16px; height: 16px;display: inline-block;">&nbsp;</div></p>

打印xpath的全部内容而不是仅打印文本的最佳方法是什么?谢谢您的帮助!

解决方法:

使用lxml.etree.tostring

print etree.tostring(result[0])
上一篇:python – 如何获取标记后的文本


下一篇:python – lxml classic:获取除嵌套标签之外的文本内容?