• 请求传参.


    请求传参使用场景

    使用场景:如果爬取解析的数据不在同一张页面中。

    Boss网站中,岗位的名称和关于岗位的描述是不在一个页面的。所以我们需要的信息在不同的页面中,所以要用到请求传参。
    请求传参的方法页不难。
    boss.py文件的parse函数中,最后一行为:
    在Request中callback回调为另外一个处理岗位详情页的函数。
    添加参数meta字典,封装已经写好部分数据的item到另外一个函数parse_detail中。

    #手动请求的发送
    #请求传参:meta={},可以将meta字典传递给请求对应的回调函数
    yield scrapy.Request(detail_url,callback=self.parse_detail,meta={'item':item})
    
    
    • 1
    • 2
    • 3
    • 4

    然后再在这个函数parse_detail中处理我们需要的另一个数据。

       #回调函数接受item
        def parse_detail(self,response):
            item = response.meta['item']
    
            job_desc = response.xpath('//*[@id="main"]/div[3]/div/div[2]/div[2]/div[1]/div//text()').extract()
            job_desc = ''.join(job_desc)
            # print(job_desc)
            item['job_desc'] = job_desc
    
            yield item
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    教训:忘记在settings.py中开启管道

    因为这里用管道将数据保存到本地,但是一开始忘记在settings.py中将管道代码取消注释!!

    boss.py爬虫文件完整代码

    此代码已失效,主要学习请求传参。

    # -*- coding: utf-8 -*-
    import scrapy
    from bossPro.items import BossproItem
    
    class BossSpider(scrapy.Spider):
        name = 'boss'
        # allowed_domains = ['www.xxx.com']
        start_urls = ['https://www.zhipin.com/job_detail/?query=python&city=101010100&industry=&position=']
    
        url = 'https://www.zhipin.com/c101010100/?query=python&page=%d'
        page_num = 2
    
       #回调函数接受item
        def parse_detail(self,response):
            item = response.meta['item']
    
            job_desc = response.xpath('//*[@id="main"]/div[3]/div/div[2]/div[2]/div[1]/div//text()').extract()
            job_desc = ''.join(job_desc)
            # print(job_desc)
            item['job_desc'] = job_desc
    
            yield item
    
        #解析首页中的岗位名称
        def parse(self, response):
            li_list = response.xpath('//*[@id="main"]/div/div[3]/ul/li')
            for li in li_list:
                item = BossproItem()
    
                job_name = li.xpath('.//div[@class="info-primary"]/h3/a/div[1]/text()').extract_first()
                item['job_name'] = job_name
                # print(job_name)
                detail_url = 'https://www.zhipin.com'+li.xpath('.//div[@class="info-primary"]/h3/a/@href').extract_first()
                #对详情页发请求获取详情页的页面源码数据
                #手动请求的发送
                #请求传参: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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    网络类型整理,看你知道几种网络
    K8s 多集群实践思考和探索
    LoRa模块空中唤醒功能原理和物联网应用
    Word控件Spire.Doc 【页面设置】教程(4) 如何在文档中插入分节符
    unidbg-补文件环境(二)
    SpringBoot-线程池ThreadPoolExecutor异步处理(包含拆分集合工具类)
    redis内存描述
    Java-图书项目(封装、继承、多态)
    数据增强系列(补充ing...)
    Hive分区表和分桶表
  • 原文地址:https://blog.csdn.net/qq_45895217/article/details/133436319