V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Colorful
V2EX  ›  Python

爬虫小白,想请教几个问题

  •  
  •   Colorful · 2020-03-04 20:50:45 +08:00 · 2327 次点击
    这是一个创建于 1507 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 scrapy 写的,碰到个问题,运行的时候,没有经过 pipelines 页面

    wincos 为主目录

    wincos/spiders/win4.py 内容是:

    -- coding: utf-8 --

    import scrapy from wincos.items import WincosItem from scrapy.http import Request

    class Win4Spider(scrapy.Spider): name = 'win4' allowed_domains = ['www.win4000.com'] start_urls = ['http://www.win4000.com/meinvtag26_1.html']

    def parse(self, response):
        mtitem = WincosItem()
        mtitem['title'] = response.xpath("//a/img/@src").extract()  #标题
        
        # http://www.win4000.com/meinv
        print("================")
        print(mtitem['title'])
        yield  mtitem
        for i in range(1,3):
            url="http://www.win4000.com/meinvtag26_"+str(i)+".html"
            print(url)
            yield Request(url,callback=self.parse)
    

    items 页面内容是: import scrapy class WincosItem(scrapy.Item): title = scrapy.Field()

    pipelines 页面是: class WincosPipeline(object): def process_item(self, item, spider): print("===========88888888============") print(item) for i in range(0,len(item['title'])): print("===========666666============") print(item['title'][i]) return item

    运行得到的数据是{'title':['所有的图片']

    但是没有进入 pipelines 里面来,不知道问题在哪。想保存数据进来

    6 条回复    2020-03-06 12:43:34 +08:00
    wuyifar
        1
    wuyifar  
       2020-03-05 11:10:00 +08:00
    settings.py 这个文件中的 ITEM_PIPELINES 设置了吗, 优先级调高一点看一下
    Colorful
        2
    Colorful  
    OP
       2020-03-05 16:15:15 +08:00
    @wuyifar 可以了,太感谢了
    Colorful
        3
    Colorful  
    OP
       2020-03-05 16:24:55 +08:00
    @wuyifar 还想请教下,如何把图片保存到本地?
    Dustyposa
        4
    Dustyposa  
       2020-03-05 17:04:19 +08:00
    `Path(name).write_bytes()`
    存图片
    wuyifar
        5
    wuyifar  
       2020-03-05 17:45:41 +08:00
    @Colorful 存图片的好,4 楼给了方法了, 其实就普通的文件保存的方法也可以,就直接 open
    Colorful
        6
    Colorful  
    OP
       2020-03-06 12:43:34 +08:00
    @wuyifar 我看看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2723 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 05:21 · PVG 13:21 · LAX 22:21 · JFK 01:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.