获取当前url页面ref scrapy

2024-06-28 10:57:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在我的脚本中添加刮取页面的当前url。但由于某些原因我不能选择这个:

<link rel="canonical" href="https://www.cdiscount.com/sante-mieux-vivre/hygiene-beaute-parapharmacie-bio/v-16516-16516.html" />

它嵌套在head中。在

我试过response.xpath("//head/link[@rel='canonical']@href").extract()

我做错什么了?在


Tags: https脚本comurlwwwlink原因页面
2条回答

如果您只需要当前响应的url。您可以使用response.url

如果您真的需要规范的URL,这应该可以:

response.xpath("//link[@rel='canonical']/@href").get()

您的表达式在@href之前缺少/。在

您还可以使用CSS:

^{pr2}$

如果您不关心规范的URL,那么您可以遵循上面@Yall的建议。在

相关问题 更多 >