【发布时间】:2021-11-17 11:58:15
【问题描述】:
我有以下代码,想一步一步进入网站的下一页:
import scrapy
class ZoosSpider(scrapy.Spider):
name = 'zoos'
allowed_domains = ['https://www.tripadvisor.co.uk']
start_urls = ['https://www.tripadvisor.co.uk/Attractions-g186216-Activities-c48-a_allAttractions.true-United_Kingdom.html']
def parse(self, response):
tmpSEC = response.xpath("//section[@data-automation='AppPresentation_SingleFlexCardSection']")
for elem in tmpSEC:
yield {
"link": response.urljoin(elem.xpath(".//a/@href").get())
}
nextPage = response.xpath("//a[@aria-label='Next page']/@href").get()
if nextPage != None:
nextPage = response.urljoin(nextPage)
yield scrapy.Request(nextPage, callback=self.parse)
但是当我运行这段代码时,只有第一页被抓取,我得到这个错误消息:
2021-11-17 12:52:20 [scrapy.spidermiddlewares.offsite] DEBUG: Filtered offsite request to 'www.tripadvisor.co.uk': <GET https://www.tripadvisor.co.uk/ClientLink?value=NVB5Xy9BdHRyYWN0aW9ucy1nMTg2MjE2LUFjdGl2aXRpZXMtYzQ4LWFfYWxsQXR0cmFjdGlvbnMudHJ1ZS1vYTMwLVVuaXRlZF9LaW5nZG9tLmh0bWxfQ3Yx>
只有当我删除这一行时,我才会得到所有结果
allowed_domains = ['https://www.tripadvisor.co.uk']
为什么 - 指向以下站点的链接具有允许的域?
【问题讨论】:
-
allowed_domains 只接受域,不接受 URL。使用“www.tripadvisor.co.uk”
标签: python web-scraping scrapy