zoukankan      html  css  js  c++  java
  • scrapy 请求传参

    class MovieSpider(scrapy.Spider):
        name = 'movie'
        allowed_domains = ['www.id97.com']
        start_urls = ['http://www.id97.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('.//h1/a/text()').extract_first()
                item['score'] = div.xpath('.//h1/em/text()').extract_first()
                #xpath(string(.))表示提取当前节点下所有子节点中的数据值(.)表示当前节点
                item['kind'] = div.xpath('.//div[@class="otherinfo"]').xpath('string(.)').extract_first()
                item['detail_url'] = div.xpath('./div/a/@href').extract_first()
                #请求二级详情页面,解析二级页面中的相应内容,通过meta参数进行Request的数据传递
                yield scrapy.Request(url=item['detail_url'],callback=self.parse_detail,meta={'item':item})
    
        def parse_detail(self,response):
            #通过response获取item
            item = response.meta['item']
            item['actor'] = response.xpath('//div[@class="row"]//table/tr[1]/a/text()').extract_first()
            item['time'] = response.xpath('//div[@class="row"]//table/tr[7]/td[2]/text()').extract_first()
            item['long'] = response.xpath('//div[@class="row"]//table/tr[8]/td[2]/text()').extract_first()
            #提交item到管道
            yield item
  • 相关阅读:
    poj 1456 贪心+STL
    hdu 4283 区间dp
    hdu 4745 区间dp
    hdu 3652 数位dp
    poj 2955 区间dp
    ubuntu apache2配置详解(含虚拟主机配置方法)
    从 mysql 客户端导出数据库 mysqldump
    Git 版本回退问题详解
    SEO 外链 内链 的定义
    为项目编写Readme.MD文件
  • 原文地址:https://www.cnblogs.com/ls1997/p/10874556.html
Copyright © 2011-2022 走看看