来自lxml html的Python Print元素

时间:2016-02-02 02:08:03

标签: python lxml.html

尝试打印出从lxml中检索到的整个元素。

from lxml import html
import requests

page=requests.get("http://finance.yahoo.com/q?s=INTC")
qtree = html.fromstring(page.content)

quote = qtree.xpath('//span[@class="time_rtq_ticker"]')

print 'Shares outstanding', Shares
print 'Quote', quote

我得到的输出是

Quote [<Element span at 0x1044db788>]

但是我想打印出用于故障排除的元素。

1 个答案:

答案 0 :(得分:4)

tostring()

中有功能lxml.html
import lxml, lxml.html

print lxml.html.tostring(element)

-

xpath会返回列表,因此您必须使用[0]

print 'Quote', html.tostring(quote[0])

for循环

for x in quote:
    print 'Quote', html.tostring(x)