请求传参

在某些情况下,我们爬取的数据不在同一个页面中,例如,我们爬取一个电影网站,电影的名称,评分在一级页面,而要爬取的其他电影详情在其二级子页面中。这时我们就需要用到请求传参

 案例展示:爬取http://www.55xia.com电影网,将一级页面中的电影名称,名字,评分     二级页面中的导演,演员进行爬取

爬虫文件

# -*- coding: utf-8 -*-
import scrapy

from moviePro.items import MovieproItem
class MovieSpider(scrapy.Spider):
    name = 'movie'
    # allowed_domains = ['www.xxx.com']
    start_urls = ['http://www.55xia.com/']

    def parse(self, response):
        div_list = response.xpath('//div[@class="col-xs-1-5 movie-item"]')
        for div in div_list:
            item = MovieproItem()
            item['name'] = div.xpath('.//div[@class="meta"]/h1/a/text()').extract_first()
            item['score'] = div.xpath('.//div[@class="meta"]/h1/em/text()').extract_first()
            if item['score'] == None:
                item['score'] = '0'
            detail_url = 'https:'+div.xpath('.//div[@class="meta"]/h1/a/@href').extract_first()

            #对详情页的url发请求
            #使用meta参数实现请求传参
            yield scrapy.Request(url=detail_url,callback=self.getDetailPage,meta={'item':item})

    def getDetailPage(self,response):
        item = response.meta['item']
        deactor = response.xpath('/html/body/div[1]/div/div/div[1]/div[1]/div[2]/table/tbody/tr[1]/td[2]/a/text()').extract_first()
        desc = response.xpath('/html/body/div[1]/div/div/div[1]/div[2]/div[2]/p/text()').extract_first()
        item['desc'] = desc
        item['deactor'] =deactor

        yield item


        #总结:当使用scrapy进行数据爬取的时候,如果发现爬取的数据值没有在同一张页面中进行存储.则必须使用请求传参进行处理(持久化)
movie

相关文章:

  • 2022-01-02
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2022-02-21
  • 2022-12-23
  • 2021-09-28
  • 2022-01-16
  • 2022-12-23
相关资源
相似解决方案