我已经用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>
(我要摘取标题:popularous) 当我提取的每个标题都有一个href改变时,我在上面的例子中尝试了下面的方法,希望它能看到类,这就足够了,但是这不起作用
titles = tree.xpath('//a[@class="product-name"]/text()')
我在寻找一个像*这样的角色,比如“我不在乎这里有什么,只要用a href=”把所有东西都拿走。。但什么也找不到
titles = tree.xpath('//a[@href="*"]/text()')
另外,我是否需要指定
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&controller=product&id_lang=7" title="4 in 1 fun pack">4 in 1 fun pack</a></h3>
试试这个:
注意类选择器后面的
//
。你知道吗相关问题 更多 >
编程相关推荐