在抓取的 href 链接之前添加 'https:'?

Add 'https:' before scraped href links?

我正在尝试抓取产品详细信息 URL 并转到相应的页面。 我有

product_detail_link = product.css('.title-selling-point a::attr(href)').extract()

此代码将得到正确的 link:

'//product.suning.com/0000000000/11346320883.html'

但是,因为前面没有'http:',当我运行

yield scrapy.Request(product_detail_link, callback=self.start_scraping)

,无法进入相应页面。 如何在我抓取的 link 前面添加一个 'https:'? 我试过了

yield scrapy.Request('https:'+product_detail_link, callback=self.start_scraping)

product_detail_link = 'https:'+product.css('.title-selling-point a::attr(href)').extract()

但是两者都不行,我猜是因为 href 不是字符串所以我不能在它前面添加一些东西?知道如何解决吗?

您正在使用 extract 方法,其中 returns 个 link 列表而不是一个 link。因此,您需要遍历所有 links 并将 httphttps 添加到所有 links.

如果你需要一个link,你应该使用get()extract_first()方法