Python LXML-返回空列表 [英] Python lxml - returns null list
问题描述
当尝试从网页表中提取值时,我无法弄清楚XPATH出了什么问题.该方法似乎是正确的,因为我可以提取页面标题和其他属性,但是我不能提取第三个值,它总是返回一个空列表?
I cannot figure out what is wrong with the XPATH when trying to extract a value from a webpage table. The method seems correct as I can extract the page title and other attributes, but I cannot extract the third value, it always returns an empty list?
from lxml import html
import requests
test_url = 'SC312226'
page = ('https://www.opencompany.co.uk/company/'+test_url)
print 'Now searching URL: '+page
data = requests.get(page)
tree = html.fromstring(data.text)
print tree.xpath('//title/text()') # Get page title
print tree.xpath('//a/@href') # Get href attribute of all links
print tree.xpath('//*[@id="financial"]/table/tbody/tr/td[1]/table/tbody/tr[2]/td[1]/div[2]/text()')
除非我丢失了某些内容,否则XPATH似乎是正确的:
Unless i'm missing something, it would appear the XPATH is correct:
我检查了Chrome控制台,看起来没问题!所以我很茫然
I checked Chrome console, appears ok! So i'm at a loss
$x ('//*[@id="financial"]/table/tbody/tr/td[1]/table/tbody/tr[2]/td[1]/div[2]/text()')
[
"£432,272"
]
推荐答案
您应指定元素名称.如果您不想指定特定的标签名称,则可以使用*
:
You should specify element name. If you don't want specify specific tag name, you can use *
:
print tree.xpath('//*[@id="financial"]/...')
^
更新
在html文件中(只是在浏览器中呈现之前的html),没有tbody标签.因此,您需要从表达式中删除tbody
:
In the html file (just the html before the rendering in the browser), there's no tbody tag. So you need to remove tbody
from the expression:
//*[@id="financial"]/table/tr/td[1]/table/tr[2]/td[1]/div[2]/text()
使用following-sibling
轴的替代方式:
//div[text()="Total Assets"]/following-sibling::div/text()
这篇关于Python LXML-返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!