语法错误:'yield'在函数Python蜘蛛脚本外部- Scrapy

hm2xizp9  于 2022-11-09  发布在  Python
关注(0)|答案(1)|浏览(176)

我尝试运行以下蜘蛛脚本来抓取geekbuying列表数据,但遇到了错误-语法错误:'yield'在函数之外


# -*- coding: utf-8 -*-

import scrapy

class FlashDealsSpider(scrapy.Spider):
    name = 'flash_deals'
    allowed_domains = ['www.geekbuying.com']
    start_urls = ['https://www.geekbuying.com/deals/categorydeals']

    def parse(self, response):
        items = response.xpath("//div[@class='flash_li']")
        for product in items:
            product_name = product.xpath(".//a[@class='flash_li_link']/text()").get()
            product_sale_price = product.xpath(" .//div[@class='flash_li_price']/span/text()").get()
            product_org_price = product.xpath(".//div[@class='flash_li_price']/del/text()").get()
            product_url = product.xpath(".//a[@class='flash_li_link']/@href").get()
            discount =  product.xpath(".//div[@class='category_li_off']/text()").get()
        yield
        {
            'name': product_name,
            'sale_price': product_sale_price,
            'orginal_price': product_org_price,
            'url': product_url,
            'discount': discount
        }

        next_page = response.xpath("//a[@class='next']/@href").get()
        if next_page:
            yield response.follow(url=next_page, callback=self.parse)

有人知道如何解决这个语法错误吗?

q35jwt9p

q35jwt9p1#

yield用于在函数内部停止其执行临时暂停其执行并将控制权转移给函数的调用方。
首先,你发布的代码并没有产生你所说的错误,因为在编写问题时,你实际上在函数体中缩进了yeild关键字。
但是你的程序将不会按预期运行,因为它是语义错误的。你需要在for循环中进一步缩进yield

import scrapy

class FlashDealsSpider(scrapy.Spider):
    name = 'flash_deals'
    allowed_domains = ['www.geekbuying.com']
    start_urls = ['https://www.geekbuying.com/deals/categorydeals']

    def parse(self, response):
        items = response.xpath("//div[@class='flash_li']")
        for product in items:
            product_name = product.xpath(".//a[@class='flash_li_link']/text()").get()
            product_sale_price = product.xpath(" .//div[@class='flash_li_price']/span/text()").get()
            product_org_price = product.xpath(".//div[@class='flash_li_price']/del/text()").get()
            product_url = product.xpath(".//a[@class='flash_li_link']/@href").get()
            discount =  product.xpath(".//div[@class='category_li_off']/text()").get()
             yield {
                    'name': product_name,
                    'sale_price': product_sale_price,
                    'orginal_price': product_org_price,
                    'url': product_url,
                    'discount': discount
             }

        next_page = response.xpath("//a[@class='next']/@href").get()
        if next_page:
            yield response.follow(url=next_page, callback=self.parse)

相关问题