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
vcent
V2EX  ›  Python

小白学 Scrapy 框架求解答问题

  •  
  •   vcent · 2018-04-02 21:37:17 +08:00 · 1655 次点击
    这是一个创建于 2208 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请问 spider 里面俩处理函数 第一个函数有循环每循环一次 yield 一个 Request,callback 第二个函数,scrapy 框架执行是先把第一个函数循环循环完然后按照队列区执行第二个函数?还是 yield 完也该就去执行第二个 第一个也同时进行着??具体问题如下:爬取豆瓣 250 第一页电影,后两个参数 film_info,play_link 是进详情页面获取的,调试发现是先循环完第一个函数在执行第二个函数的,所以给第二个函数的参数是第一个函数最后一次循环结果导致数据重复严重。Scrapy 不是异步的????求解答 class TestSpider(scrapy.Spider): name = 'test' allowed_domains = ['douban.com'] start_urls = ['https://movie.douban.com/top250']

    def parse(self, response):
        item=items.DoubanItem()
        movies=response.css('.grid_view li')
        for tmp in movies:
            #排名
            item['ranking']=tmp.css('em::text').extract_first(default='')
            #电影名
            item['film_name']=tmp.css('.title::text').extract_first(default='')
            #电影精髓
            item['film_desc']=tmp.css('.inq::text').extract_first(default='')
            #分数
            item['film_score']=tmp.css('.rating_num::text').extract_first(default='')
            #封面链接
            item['film_cover']=tmp.css('img::attr(src)').extract_first(default='')
            #是否可播放
            play_button=tmp.css('.playable').extract_first(default='')
            if play_button :
                yield Request(tmp.css('.hd a::attr(href)').extract_first(),callback=self.parse_detail,meta={'key':item})
            else:
                item['play_link'],item['film_info']=('N/A','N/A')
                yield item
            
    def parse_detail(self,response):
        result=copy.deepcopy(response.meta['key'])
        result['film_info']=response.css('[property="v:summary"]::text').extract_first(default='').strip()
        resource_list=response.css('.bs li')
        waylist=[]
        for tmp in resource_list:
            resource=tmp.css('a::text').extract_first(default='').strip()
            vip=tmp.css('span span::text').extract_first(default='').strip()
            waylist.append(resource+vip)
        result['play_link']=','.join(waylist)
        yield result
    

    pipline 就是简单的存储进 mongodb,结果是
    数据都是重复的,除了第二个函数的两个字段不重复其他字段都和第 25 调数据一样额,,,,表达不清楚,如果有那位好心人可以帮忙看下,受累加 Q 1009377243 多谢。。实在没招拉

    1 条回复    2018-04-03 17:55:37 +08:00
    farverfull
        1
    farverfull  
       2018-04-03 17:55:37 +08:00
    你把 item=items.DoubanItem()放进 for in 里,但不建议这么做,meta 直接传递数据吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3052 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:52 · PVG 22:52 · LAX 07:52 · JFK 10:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.