尝试打印出从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>]
但是我想打印出用于故障排除的元素。
答案 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)