使用更改href的Python web抓取

使用更改href的Python web抓取,python,web-scraping,tags,href,Python,Web Scraping,Tags,Href,我一直在使用Python2.7抓取一些网站 page = requests.get(URL) tree = html.fromstring(page.content) prices = tree.xpath('//span[@class="product-price"]/text()') titles = tree.xpath('//span[@class="product-title"]/text()') 这适用于包含这些清晰标记的网站,但我遇到的许多网站都

我一直在使用Python2.7抓取一些网站

    page = requests.get(URL)
    tree = html.fromstring(page.content)

    prices = tree.xpath('//span[@class="product-price"]/text()')
    titles = tree.xpath('//span[@class="product-title"]/text()')
这适用于包含这些清晰标记的网站,但我遇到的许多网站都有以下HTML设置:

<a href="https://www.retronintendokopen.nl/gameboy/games/gameboy-classic/populous" class="product-name"><strong>Populous</strong></a>
我在搜索一个类似*的角色,比如“我不在乎这里有什么,只要用a href=”把所有东西都拿走就行了”。。但是什么也找不到

titles = tree.xpath('//a[@href="*"]/text()')
另外,我是否需要在a标记中指定class=,如

titles = tree.xpath('//a[@href="*" @class="product-name"]/text()')
编辑:我还发现了一个修复方法,如果a路径中只有更改的标记,请使用

titles = tree.xpath('//h3/a/@title')
此标记的示例

<h3><a href="http://www.a-retrogame.nl/index.php?id_product=5843&amp;controller=product&amp;id_lang=7" title="4 in 1 fun pack">4 in 1 fun pack</a></h3>

试试这个:

titles = tree.xpath('//a[@class="product-name"]//text()')

注意类选择器后面的
/

试试beautifulsoup和@nishantkumar no!beautifulsoup不是理想的刮削解决方案<代码>XPath是!。试试刮痧。另外,在xpath中,//a[@href]被用来证明存在这太简单了哈哈,你能解释一下双精度运算符实际做了什么,而单精度运算符不起作用吗?@Alex double
/
的意思是
任何间接子精度
,即你可以观察到
之后有
。因此,xpath无法找到您的
文本
内容,因为它希望它是
立即的
子内容。这就是为什么我们需要
/
。希望天气转晴
titles = tree.xpath('//a[@class="product-name"]//text()')