Python Xpath:lxml.etree.XPathEvalError:谓词无效 [英] Python Xpath: lxml.etree.XPathEvalError: Invalid predicate

查看:44
本文介绍了Python Xpath:lxml.etree.XPathEvalError:谓词无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何抓取网页,在教程中我使用下面的代码抛出了这个错误:

I'm trying to learn how to scrape web pages and in the tutorial I'm using the code below is throwing this error:

lxml.etree.XPathEvalError: Invalid predicate

我查询的网站是(不要评判我,它是在培训视频中使用的:/):https://itunes.apple.com/us/app/candy-crush-saga/id553834731

The website I'm querying is (don't judge me, it was the one used in the training vid :/ ): https://itunes.apple.com/us/app/candy-crush-saga/id553834731

导致错误的 xpath 字符串在这里:

The xpath string that causes the error is here:

links = tree.xpath('//div[@class="center-stack"//*/a[@class="name"]/@href')

我正在使用 LXML 和请求库.

I'm using the LXML and requests libraries.

如果您需要任何其他信息,我很乐意提供!

If you need any additional info I'm happy to provide!

推荐答案

print(tree.xpath('//div[@class="center-stack"]//*/a[@class="name"]/@href'))

您在 "center-stack" 之后缺少一个结束的 ].

You were missing a closing ] after "center-stack".

您也可以从 div[@class="content"]

 tree.xpath('//div[@class="content"]//a[@class="name"]/@href')

两者都会给你你想要的hrefs:

Both will give you the hrefs you want:

In [19]: import  requests

In [20]: from lxml.html import fromstring


In [21]: r = requests.get("https://itunes.apple.com/us/app/candy-crush-saga/id553834731")

In [22]: tree = fromstring(r.content)

In [23]: a = tree.xpath('//div[@class="content"]//a[@class="name"]/@href')

In [24]: b =  tree.xpath('//div[@class="center-stack"]//*/a[@class="name"]/@href')

In [25]: print(a == b)
True

In [26]: print(a)
['https://itunes.apple.com/us/app/word-search-puzzles/id609067187?mt=8', 'https://itunes.apple.com/us/app/cookie-jam/id727296976?mt=8', 'https://itunes.apple.com/us/app/jewel-mania/id561326449?mt=8', 'https://itunes.apple.com/us/app/jelly-splash/id645949180?mt=8', 'https://itunes.apple.com/us/app/bubble-island/id531354582?mt=8']

In [27]: print(b)
['https://itunes.apple.com/us/app/word-search-puzzles/id609067187?mt=8', 'https://itunes.apple.com/us/app/cookie-jam/id727296976?mt=8', 'https://itunes.apple.com/us/app/jewel-mania/id561326449?mt=8', 'https://itunes.apple.com/us/app/jelly-splash/id645949180?mt=8', 'https://itunes.apple.com/us/app/bubble-island/id531354582?mt=8']

这篇关于Python Xpath:lxml.etree.XPathEvalError:谓词无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆