有没有一种方法可以让元素的url显示为<a href =“#”>

时间:2019-04-18 12:39:30

标签: python xpath scrapy

当我们第一次访问产品页面网址时,我正试图从Bestbuy.com(美国)网站上抓取数据。它要求我们选择国家(product page)。当用户点击美国国旗时,我试图获取指向美国的页面的URL

注意:您可以尝试在隐身窗口中打开产品页面的链接

我们必须在其中选择国家/地区的零售商网站上的元素的代码段如下:

<div class="country-selection">
<a href="#" class="us-link">
<img src="https://assets.bbystatic.com/international/landing_page/v2/images/maps/usa.svg" alt="United States">
<h4>United States</h4>
</a>
</div>

当我运行scrapy命令获取href元素的url时,我得到的值为'#',这是正确的

response.xpath('/html/body/div[2]/div/div/div/div[1]/div[2]/a[2]/@href').extract()

当我将鼠标悬停在源代码中的“#”上时,可以看到URL,该如何获取该值?

图片>> image Link

1 个答案:

答案 0 :(得分:0)

正如Luiz所说,response.url包含当前URL。

如果您通常想将相对URL解析为绝对URL,则利用response.urljoin()或将Request()替换为response.follow()可能是一种更好的方法。

相关问题