scrapy中对于item的把控

其实很简单,就是想要存储的位置发生改变。直接看例子,然后触类旁通。

以大众点评 评论的内容为例 ,位置:http://www.dianping.com/shop/77489519/review_more?pageno=1

数据存储形式由A 变成B

A:

展开的话这样子:

B:

本质上看,就是多个相同类型的item可以合并,不需要那么多,分别来看下各自的代码:

A:

class GengduopinglunSpider(scrapy.Spider):
    name = ‘gengduopinglun‘
    start_urls = [‘http://www.dianping.com/shop/77489519/review_more?pageno=1‘]

    def parse(self, response):
        item=PinglunItem()
        comment = item[‘comment‘] if "comment" in item else []
        for i in response.xpath(‘//div[@class="content"]‘):
            for j in i.xpath(‘.//div[@class="J_brief-cont"]/text()‘).extract():
                comment.append(j.strip())
        item[‘comment‘]=comment
        next_page = response.xpath(
            ‘//div[@class="Pages"]/div[@class="Pages"]/a[@class="NextPage"]/@href‘).extract_first()
        item[‘_id‘]=next_page
        # item[‘_id‘]=‘onlyone‘
        if next_page != None:
            next_page = response.urljoin(next_page)
            # yield Request(next_page, callback=self.shop_comment,meta={‘item‘: item})
            yield Request(next_page, callback=self.parse,)
        yield item

B:

class GengduopinglunSpider(scrapy.Spider):
    name = ‘gengduopinglun‘
    start_urls = [‘http://www.dianping.com/shop/77489519/review_more?pageno=1‘]

    def parse(self, response):
        item=PinglunItem()
        comment = item[‘comment‘] if "comment" in item else []
        for i in response.xpath(‘//div[@class="content"]‘):
            for j in i.xpath(‘.//div[@class="J_brief-cont"]/text()‘).extract():
                comment.append(j.strip())
        item[‘comment‘]=comment
        next_page = response.xpath(
            ‘//div[@class="Pages"]/div[@class="Pages"]/a[@class="NextPage"]/@href‘).extract_first()
        # item[‘_id‘]=next_page
        item[‘_id‘]=‘onlyone‘
        if next_page != None:
            next_page = response.urljoin(next_page)
            yield Request(next_page, callback=self.shop_comment,meta={‘item‘: item})
            # yield Request(next_page, callback=self.parse,)
        # yield item

    def shop_comment(self, response):
        item = response.meta[‘item‘]
        comment = item[‘comment‘] if "comment" in item else []
        for i in response.xpath(‘//div[@class="content"]‘):
            for j in i.xpath(‘.//div[@class="J_brief-cont"]/text()‘).extract():
                comment.append(j.strip())
        item[‘comment‘]=comment
        next_page = response.xpath(
            ‘//div[@class="Pages"]/div[@class="Pages"]/a[@class="NextPage"]/@href‘).extract_first()
        if next_page != None:
            next_page = response.urljoin(next_page)
            yield Request(next_page, callback=self.shop_comment,meta={‘item‘: item})
        yield item

B里面是有重复代码的,这个无关紧要,只是演示,注意看两个yield 的区别

以上只是演示scrapy中yield的用法,用来控制item,其余pipline,setting未展示.

时间: 2024-07-29 11:25:52

scrapy中对于item的把控的相关文章

关于ListView中item与子控件抢夺焦点的解决方法

1.在开发中,listview可以说是我们使用最频繁的控件之一了,但是关于listview的各种问题也是很多.当我们使用自定义布局的Listview的时候,如果在item的布局文件里面存在Button或者是CheckBox等控件以及其子类控件的时候,经常会碰到各种控件的点击事件冲突的情况,那么我们如何来处理Listview中这种控件之间焦点冲突的情况呢? 这里有三种解决方案 1.将ListView中的Item布局中的子控件focusable属性设置为false2.在getView方法中设置but

Scrapy中对xpath使用re

Scrapy中使用xpath时,根据xpath的语法不一定能得到想要的. 如下面的html源码: 1 <div class="db_contout"> <div class="db_cont"> <div class="details_nav"> <a href="http://movie.mtime.com/79055/addimage.html" class="db_ad

抓取scrapy中文文档 第一个Scrapy项目实现

使用Scrapy这个python的网络爬虫框架抓取Scrapy中文文档 开发第一步:新建项目scrapy startproject myfirst 目录结构: myfirst │ scrapy.cfg Scrapy项目配置文件 │ └─myfirst Scrapy项目代码存放目录 │ items.py 存储从抓取的网页中需要保存的数据,再其中指定要存储的域 │ pipelines.py 管道文件,用于存储从抓取的网页中解析出的其他页面的url,相当于任务队列 │ settings.py 爬虫配置

[转]scrapy中的request.meta

作者:知乎用户链接:https://www.zhihu.com/question/54773510/answer/146971644 meta属性是字典,字典格式即{'key':'value'},字典是一种可变容器模型,可存储任意类型对象. request中meta参数的作用是传递信息给下一个函数,这些信息可以是任意类型的,比如值.字符串.列表.字典......方法是把要传递的信息赋值给meta字典的键,分析见如下语句(爬虫文件): class example(scrapy.Spider): n

锐浪 报表, 当多行交叉报表时,对多行交叉报表中自由格中的多个字段控件,进行颜色控制. 取值 判断等实现

需要注意的点是: 1 . 要对自由格中对应的 字段框的背景填充方式改为:填充,否则没有效果. 2 . 代码中红色部门代码: Column 为明细网络对象属性中的 列集合 中的 交叉列的 名称,  Report.RunningDetailGrid.Columns.Item("Column_2") 3.  蓝色部门 为自由格中 控件的索引位置 ,也可以根据字段框的名称来进行控制如:  contentCell.Controls.Item("FieldBox8") 代码如下

android中在xml文件中使用View在某个控件的上方画一条线;android:listSelector的属性说明;android:visibility=&quot;gone&quot;

一.例如RelativeLayout中android:visibility="gone" 其有三个属性:visible显示:invisible显示黑背景条:gone不显示 在类中,可以设置其显示与否,setVisibility(View.GONE);不显示 setVisibility(View.VISIBLE);显示 二. android:listSelector的属性说明 <GridView android:id="@+id/gridview_numberkeyboa

RecyclerView的Item和Item内的控件点击处理

需求场景:RecyclerView的Item需要点击,或者Item中的某个控件需要点击,或者两者同时需要点击处理. 一.adapter代码如下: package com.ldw.adapter; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import andr

scrapy 中 settings.py 中字段的意思

# -*- coding: utf-8 -*- # Scrapy settings for fenbushi project## For simplicity, this file contains only settings considered important or# commonly used. You can find more settings consulting the documentation:## https://doc.scrapy.org/en/latest/topi

论Scrapy中的数据持久化

引入 Scrapy的数据持久化,主要包括存储到数据库.文件以及内置数据存储. 那我们今天就来讲讲如何把Scrapy中的数据存储到数据库和文件当中. 终端指令存储 保证爬虫文件的parse方法中有可迭代类型对象(通常为列表or字典)的返回,该返回值可以通过终端指令的形式写入指定格式的文件中进行持久化操作. # 执行输出指定格式进行存储:将爬取到的数据写入不同格式的文件中进行存储 scrapy crawl 爬虫名称 -o xxx.json # 存为json文件 scrapy crawl 爬虫名称 -