我尝试运行以下蜘蛛脚本来抓取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)
有人知道如何解决这个语法错误吗?
1条答案
按热度按时间q35jwt9p1#
yield
用于在函数内部停止其执行临时暂停其执行并将控制权转移给函数的调用方。首先,你发布的代码并没有产生你所说的错误,因为在编写问题时,你实际上在函数体中缩进了
yeild
关键字。但是你的程序将不会按预期运行,因为它是语义错误的。你需要在
for
循环中进一步缩进yield
。