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

请问一下, scrapy 在一个 def 里不能发多个 yield Request 请求么?为什么?

  •  
  •   spotfg · 2019-02-02 22:13:41 +08:00 · 3056 次点击
    这是一个创建于 1903 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是我写的 spider 文件,下面有我碰到的在一个页面内所需要的值,放在 3 个 json 文件中。。。。。所以我在一个函数下写了三个 yield Request 请求……

    import scrapy from scrapy.http import Request import re from jingdong.items import JingdongItem from jingdong.settings import *

    class GoodsSpider(scrapy.Spider): name = 'goods' allowed_domains = ['jd.com'] start_urls = ['http://jd.com/']

    # https://search.jd.com/Search?keyword=图书&enc=utf-8&wq=图书&page=1
    
    # 搜索的起始页
    url = "https://search.jd.com/Search?keyword={KEYWORDS}&enc=utf-8&wq={KEYWORDS}&page={page}"
    
    # 电子价格
    Eprice_url = "https://c.3.cn/book?skuId={skuId}&cat={cat}&area=1_72_2799_0&callback=book_jsonp_callback"
    
    # 商品价格
    price_url = "https://p.3.cn/prices/mgets?type=1&area=1_72_2799_0&pdtk=&pduid=1771569446&pdpin=&pdbp=0&skuIds=J_{skuId}&ext=11100000&callback=jQuery3021180&_=1547383556702"
    price2_url = 'https://c0.3.cn/stock?skuId={skuId}&venderId=1000005720&cat={cat}&area=1_72_2799_0&buyNum=1&extraParam={%22originid%22:%221%22}&ch=1&pduid=1771569446&pdpin=&fqsp=0&callback=getStockCallback'
    
    # 评论
    comment_url = "https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv39228&productId={skuId}&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1"
    
    def start_requests(self):
        for k in range(1,PAGE_NUM):
            yield  Request(url=self.url.format(KEYWORDS=KEYWORDS,page=2*k-1),callback=self.page_parse)
    
    
    def page_parse(self, response):
        #每页商品 ID
        goodsID = response.xpath('//li/@data-sku').extract()
        print(goodsID)
    
        for each in goodsID:
            goodsurl = "https://item.jd.com/{}.html".format(each)
            yield Request(url=goodsurl,callback=self.get_goods_info)
    
    def get_goods_info(self,response):
    
        item = JingdongItem()
    
        #图书链接
        item["link"] = response.url
    
        #图书标题
        item["title"] = response.xpath('//div[@class="sku-name"]/text()').extract()[0].strip()
    
        #作者
        item["writer"] = response.xpath('//div[@class="p-author"]/a/text()').extract()
    
        #提取商品 ID
        skuId = re.compile(r'https:..item.jd.com.(\d+).html').findall(response.url)[0]
        item['Id'] = skuId
        cat = re.compile(r'pcat:\[(.*?)\],').findall(response.text)
        cat = re.sub("\|",",",cat[0]).strip("'")
        item['catId'] = cat
        print(skuId)
        print(cat)
    
        # 打开电子书价格
        yield Request(url = self.Eprice_url.format(skuId=skuId, cat=cat),meta={'item':item},callback=self.price_parse)
        # 打开原价及京东价
        yield Request(url = self.price_url.format(skuId=skuId),meta={'item':item},callback=self.jingdong_price_parse)
        # 打开评论 json
        yield Request(url = self.comment_url.format(skuId=skuId),meta={'item':item},callback=self.comment_parse)
    
    
    
    def price_parse(self,response):
        item =response.meta['item']
        #电子书价格
        item["e_price"] = re.compile('"p":"(.*?)",').findall(response.text)[0]
        yield item
    
    def jingdong_price_parse(self,response):
        item = response.meta['item']
        #京东价
        item["n_price"] = re.compile('"op":"(.*?)",').findall(response.text)[0]
        print(item["n_price"])
        #原价
        item["o_price"] = re.compile('"m":"(.*?)",').findall(response.text)[0]
    
        if item["n_price"] == None and item["o_price"]== None:
            yield Request(url=self.price2_url.format(skuId=item['id'],cat=item['catId']),meta={'item':item}, callback=self.jingdong_price_parse)
        else:
            yield item
    
    
    def comment_parse(self,response):
        item = response.meta['item']
        #评论数
        item["comment"] =re.compile('"content":"(.*?)",').findall(response.text)
    
        yield item
    

    如题目说所,这三个请求 # 打开电子书价格 yield Request(url = self.Eprice_url.format(skuId=skuId, cat=cat),meta={'item':item},callback=self.price_parse) # 打开原价及京东价 yield Request(url = self.price_url.format(skuId=skuId),meta={'item':item},callback=self.jingdong_price_parse) # 打开评论 json yield Request(url = self.comment_url.format(skuId=skuId),meta={'item':item},callback=self.comment_parse)

    将会只执行最下面的,另外两个获取不到值。我试过单独写一个爬虫来同相同的匹配规则是可以正常获取到的。所以,百思不得其解啊!!!!!

    skyqqcc
        1
    skyqqcc  
       2019-02-03 00:53:21 +08:00 via Android
    答:阔以,因为 yield 就相当于向队列里添加一个任务...其实你只要在 yield 后面加个 print 就能知道 yield 后面的代码是否被执行...😂可能队列这个想法我理解的不太对....但是窝也只写过几天 Python 然后就搁浅了?😂😂😂
    skyqqcc
        2
    skyqqcc  
       2019-02-03 00:53:58 +08:00 via Android
    还有就是你可以写个简单的 demo 试试,😂😂😂
    zyj321321321
        3
    zyj321321321  
       2019-02-03 09:35:52 +08:00   ❤️ 1
    你的问题不是导致错误的原因,scrapy 在一个 def 里是可以发送多个 yield Request 的请求的。大概率问题是在后续处理 item 的问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   901 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:05 · PVG 06:05 · LAX 15:05 · JFK 18:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.