Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python绝对XPath返回空列表,通用查询更好吗?_Python_Html_List_Xpath - Fatal编程技术网

python绝对XPath返回空列表,通用查询更好吗?

python绝对XPath返回空列表,通用查询更好吗?,python,html,list,xpath,Python,Html,List,Xpath,我希望使用XPath从html页面获取文本。 特定文本位于源url描述右侧的td中:(在th元素内) 在第一次调用(注释掉)中,我尝试了从Chrome inspector获取的XPath的绝对路径,但得到了一个空列表。 下一个调用将起作用并给出标题: “说明:” 我需要一个通用XPath查询,该查询将采用文本标题(如“Description:”),并在其旁边给出td的文本值 url = 'http://datrack.canterbury.nsw.gov.au/cgi/datrack.pl?cm

我希望使用XPath从html页面获取文本。 特定文本位于源url描述右侧的td中:(在th元素内)

在第一次调用(注释掉)中,我尝试了从Chrome inspector获取的XPath的绝对路径,但得到了一个空列表。 下一个调用将起作用并给出标题: “说明:”

我需要一个通用XPath查询,该查询将采用文本标题(如“Description:”),并在其旁边给出td的文本值

url = 'http://datrack.canterbury.nsw.gov.au/cgi/datrack.pl?cmd=download&id=ZiFfLxV6W1xHWBN1UwR5SVVSAV0GXUZUcGFGHhAyTykQAG5CWVcARwM='
page = requests.get(url)
tree = html.fromstring(page.content)

# desc = tree.xpath('//*[@id="documentpreview"]/div[1]/table[1]/tbody/tr[2]/td//text()')

desc = tree.xpath("//text()[contains(., 'Description:')]")
我尝试过各种XPath查询,但我的知识还不够深入。 任何帮助都将不胜感激。

使用
/*[contains(text(),'Description:')]
查找其文本包含
Description:
的标记,并使用
following sibling::td
查找以下属于
td
标记的同级标记:

In [180]: tree.xpath("//*[contains(text(), 'Description:')]/following-sibling::td/text()")
Out[180]: ['Convert existing outbuilding into a recreational area with bathroom and kitchenette']

我测试过了,它成功了!谢谢你给我指出这个兄弟符号unutbu。