scrapy框架请求传参处理(请求多个页面)
2022/5/24 23:50:04
本文主要是介绍scrapy框架请求传参处理(请求多个页面),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前提
如果爬取解析的数据不在同一张页面中。(深度爬取)
实战
使用古诗词网站进行模拟
import scrapy from bossPro.items import BossproItem class BossSpider(scrapy.Spider): name = 'boss' # allowed_domains = ['www.xxx.com'] start_urls = ['https://www.shicimingju.com/category/all'] url = 'https://www.shicimingju.com/chaxun/zuozhe/1_%d.html' page_num = 2 # 回调函数接受item def parse_detail(self, response): item = response.meta['item'] detail = response.xpath('//*[@id="main_right"]/div[1]/div[2]/div[1]/div/text()').extract() detail = ''.join(detail) # print(job_desc) item['detail'] = detail yield item #数据解析处理 def parse(self, response): list_data = response.xpath('//*[@id="main_left"]/div') for li in list_data: name = li.xpath('./div[@class="zuozhe_list_item"]/h3/a/text()').extract_first() detail_url = li.xpath('./div[@class="zuozhe_list_item"]/h3/a/@href').extract_first() detail_url = 'https://www.shicimingju.com' + str(detail_url) #有空值需要处理下,如果没有空值可以不用str item = BossproItem() item['name'] = name # 对详情页发请求获取详情页的页面源码数据 # 手动请求的发送 # 请求传参:meta={},可以将meta字典传递给请求对应的回调函数 yield scrapy.Request(detail_url, callback=self.parse_detail, meta={'item': item}) #分页操作 if self.page_num <= 3: new_url = format(self.url%self.page_num) self.page_num += 1 yield scrapy.Request(new_url,callback=self.parse)
这篇关于scrapy框架请求传参处理(请求多个页面)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升