设为首页 加入收藏

TOP

Scrapy各部分运行机制?Xpath为None?多层Response如何编写?搞定Scrapy的坑(二)
2019-09-14 00:53:31 】 浏览:52
Tags:Scrapy 部分 运行 机制 Xpath None 多层 Response 如何 编写 搞定 的坑
m
wallpaper.items import WallpaperItem class WallspiderSpider(scrapy.Spider): name = 'wallspider' allowed_domains = ['wall.alphacoders.com'] start_urls = ['https://wall.alphacoders.com/'] def parse(self, response): picx_list = response.xpath("//div[@class='center']//div[@class='boxgrid']/a/@href").getall() for picx in picx_list: url = 'https://wall.alphacoders.com/'+str(picx) #回调给下一层处理函数 yield scrapy.Request(url,callback=self.detail_parse) def detail_parse(self, response): pic_url = response.xpath("//*[@id='page_container']/div[4]/a/@href").get() pic_size = response.xpath("//*[@id='wallpaper_info_table']/tbody//span/span[2]/a/text()").get() pic_name = response.xpath("//*[@id='page_container']/div/a[4]/span/text()").get() wall_item = WallpaperItem() wall_item['pic_url'] = pic_url wall_item['pic_size'] = pic_size.split()[0] wall_item['pic_name'] = pic_name print(wall_item) return wall_item

同时我在csdn中看到 名为 kocor的用户写的解决方案则更有教学性,解决方案如下:

yield scrapy.Request(item['url'], meta={'item': item}, callback=self.detail_parse)

Scrapy 用scrapy.Request发起请求可以带上 meta={'item': item} 把之前已收集到的信息传递到新请求里,在新请求里用 item = response.meta('item') 接受过来,在 item 就可以继续添加新的收集的信息了。

多少级的请求的数据都可以收集。

spider.py文件

# -*- coding: utf-8 -*-
import scrapy
from Tencent.items import TencentItem
 
 
class TencentSpider(scrapy.Spider):
    # 爬虫名称
    name = 'tencent'
    # 允许爬取的域名
    allowed_domains = ['www.xxx.com']
    # 爬虫基础地址 用于爬虫域名的拼接
    base_url = 'https://www.xxx.com/'
    # 爬虫入口爬取地址
    start_urls = ['https://www.xxx.com/position.php']
    # 爬虫爬取页数控制初始值
    count = 1
    # 爬虫爬取页数 10为只爬取一页
    page_end = 1
 
    def parse(self, response):
 
 
        nodeList = response.xpath("//table[@class='tablelist']/tr[@class='odd'] | //table[@class='tablelist']/tr[@class='even']")
        for node in nodeList:
            item = TencentItem()
 
            item['title'] = node.xpath("./td[1]/a/text()").extract()[0]
            if len(node.xpath("./td[2]/text()")):
                item['position'] = node.xpath("./td[2]/text()").extract()[0]
            else:
                item['position'] = ''
            item['num'] = node.xpath("./td[3]/text()").extract()[0]
            item['address'] = node.xpath("./td[4]/text()").extract()[0]
            item['time'] = node.xpath("./td[5]/text()").extract()[0]
            item['url'] = self.base_url + node.xpath("./td[1]/a/@href").extract()[0]
            # 根据内页地址爬取
            yield scrapy.Request(item['url'], meta={'item': item}, callback=self.detail_parse)
 
            # 有下级页面爬取 注释掉数据返回
            # yield item
 
        # 循环爬取翻页
        nextPage = response.xpath("//a[@id='next']/@href").extract()[0]
        # 爬取页数控制及末页控制
        if self.count < self.page_end and nextPage != 'java script:;':
            if nextPage is not None:
                # 爬取页数控制值自增
                self.count = self.count + 1
                # 翻页请求
                yield scrapy.Request(self.base_url + nextPage, callback=self.parse)
        else:
            # 爬虫结束
            return None
 
    def detail_parse(self, response):
        # 接收上级已爬取的数据
        item = response.meta['item']   
 
        #一级内页数据提取 
        item['zhize'] = response.xpath("//*[@id='position_detail']/div/table/tr[3]/td/ul[1]").xpath('string(.)').extract()[0]
        item['yaoqiu'] = response.xpath("//*[@id='position_detail']/div/table/tr[4]/td/ul[1]").
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python 入门之 内置模块 -- os模块 下一篇Python_深拷贝和浅拷贝

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目