使用python的scrapy框架,spider与pipelines的调用

spider的程序如下:

class DmozSpider(Spider):
    name='dmoz'
    allowed_domains=['dmoz.org']
    start_urls=[
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
        ]

    def parse(self,response):
        sel=Selector(response)
        sites=sel.xpath('//ul/li')
        #inspect_response(response)
        items=[]
        #print '----------------------------------------------------'
        for site in sites:
            item=DmozItem()
            item['title']=site.xpath('a/text()').extract()
            item['link']=site.xpath('a/@href').extract()
            item['desc']=site.xpath('text()').extract()
            items.append(item)
        #print type(items),'++++++++++++++++++++++++++++++++'
        return items

pipelines的程序如下:

import json
class TutorialPipeline(object):
    def __init__(self):
        self.file=open('items.jl','w')
       
    def process_item(self, item, spider):
        #print type(item),'&&&&&&&&&&&&&&&&&&&&&&&&&&&'
        lines=json.dumps(dict(item))+"\n"
        self.file.write(lines)
        #self.file.write("\n")
        return item

 

发现如下情况:

   当经过spiders程序处理过的列表数据,会迭代逐次调用pipelines的process_item方法,可以将打印信息放出

    使用scrapy crawel  dmoz --nolog 查看日志

 

程序来自http://scrapy-chs.readthedocs.org/zh_CN/0.22/topics/item-pipeline.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值