Python Scrapy Spider-如何获取起始url并增加页面#以页面值结尾进行爬网?
我正在尝试爬网,在这个页面的底部有一个编号33400,您可以通过response.css('span.pagination_uuupages::text')。extract_first()获取该编号我想首先将其转换为33400,然后在我的代码中的start_URL中,我想从page=1增加到page=33400,然后从上面指定的css跨度中提取该值。目前,我下面的代码查找下一个页面链接并获取该href,但我在浏览所有33400个页面时遇到了问题,它只获取了大约100个页面并停止。我认为这可能是一个更好的解决方案,但我需要关于如何在代码中编写它的帮助Python Scrapy Spider-如何获取起始url并增加页面#以页面值结尾进行爬网?,python,web-scraping,scrapy,scrapy-spider,Python,Web Scraping,Scrapy,Scrapy Spider,我正在尝试爬网,在这个页面的底部有一个编号33400,您可以通过response.css('span.pagination_uuupages::text')。extract_first()获取该编号我想首先将其转换为33400,然后在我的代码中的start_URL中,我想从page=1增加到page=33400,然后从上面指定的css跨度中提取该值。目前,我下面的代码查找下一个页面链接并获取该href,但我在浏览所有33400个页面时遇到了问题,它只获取了大约100个页面并停止。我认为这可能是一
import scrapy
class QuotesSpider(scrapy.Spider):
name = "exampleurls"
allowed_domains = ["example.com"]
start_urls = ["https://www.example.com/search?format=search&page=1&query=&sort=popular&type=vector"]
def parse(self, response):
# self.log('I just visited: ' + response.url)
# for quote in response.css('div.showcase__content'):
# item = {
# 'url': quote.css('a::attr(href)').extract_first(),
# }
# yield item
urls = response.css('div.showcase__content > a::attr(href)').extract()
for url in urls:
url = response.urljoin(url)
yield scrapy.Request(url=url, callback=self.parse_details)
# follow pagination link
next_page_url = response.xpath("//a[contains(concat(' ', @class, ' '), ' pagination__next ')]/@href").extract_first()
if next_page_url:
next_page_url = response.urljoin(next_page_url)
yield scrapy.Request(url=next_page_url, callback=self.parse)
def parse_details(self, response):
yield {
'name': response.css('h1.mg-none::text').extract_first(),
'creation-date': response.xpath('//@data-creation-date').extract_first(),
'downloads': response.xpath('//@data-downloads').extract_first(),
'image-small': response.xpath('//@data-image-small').extract_first(),
'main-keyword': response.xpath('//@data-main-keywords-label').extract_first(),
'url': response.xpath("//meta[@property='og:url']/@content").extract(),
'is-premium': response.xpath('//@data-premium').extract_first(),
'is-selection': response.xpath('//@data-selection').extract_first(),
}
世界
因此,您希望通过一组数字来确定范围,将它们添加到请求中,等等,这应该非常容易
在这种情况下,最好的解决方案是使用带有链接提取器规则集的爬网爬行器。尝试启用重复调试,看看它在那里写了什么,可能是因为它按“流行”排序当crawler位于100ish页面上或仅位于33k页面上时,服务会以某种方式重新排列页面,在该限制范围内只有大约100ish和dupefilter凹凸。此外,请尝试按最近的排序或一次生成开始URL(或在基础函数中)的所有链接,例如(您在第一次开始时获得30000个数字):
all\u URL=[f'https://www.freepik.com/search?format=search&page={str(x)}&sort=recent&type=vector'用于范围(130000)内的x)
当它停止时,您会收到什么错误消息?可能是网站开始阻止您,或者您正在使服务器过载。如果这是原因,则您的number方法也可能失败。尝试启用duplicates调试并查看它在那里写了什么,可能是因为它按“流行”排序当crawler位于100ish页面上或仅位于33k页面上时,服务会以某种方式重新排列页面,在该限制范围内只有大约100ish和dupefilter凹凸。此外,请尝试按最近的排序或一次生成开始URL(或在基础函数中)的所有链接,例如(您在第一次开始时获得30000个数字):all\u URL=[f'https://www.freepik.com/search?format=search&page={str(x)}&sort=recent&type=vector'表示范围(130000)内的x)]
当它停止时,您会收到什么错误消息?可能是网站开始阻止您,或者您正在使服务器超载。如果这是原因,则您的number方法也可能失败。
start_urls = ["https://www.example.com/search?format=search&page=%s&query=&sort=popular&type=vector"% page for page in xrange(33400)]