在 python 中命名产量 return 的问题
Problem with naming the yield return in python
我正在尝试使用 python 和 scrapy 抓取网站,但我在保存结果时遇到问题。
我收到错误日志:
yield result = {
^
SyntaxError: invalid syntax
当我删除“result =”时,我没有收到任何错误,但我这样做的原因是将结果保存为我在"f.write(result)"
中代码的最后一部分
代码如下:
import scrapy
class ExampleSpider(scrapy.Spider):
name = "ufcspider"
start_urls = [
'http://quotes.toscrape.com/page/1/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield result = {
'text': quote.css('span.text::text').get(),
'author': quote.css('small.author::text').get(),
'link': 'http://quotes.toscrape.com' + quote.css("span a::attr(href)").get(),
'tags': quote.css('div.tags a.tag::text').getall(),
}
next_page = response.css("li.next a::attr(href)").get()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callable=self.parse)
page = response.url.split("/")[-2]
filename = f'quotes-{page}.json'
with open(filename, 'wb') as f:
f.write(result)
self.log(f'Saved file {filename}')
先定义result
,然后定义yield
result = { ... }
yield result
我正在尝试使用 python 和 scrapy 抓取网站,但我在保存结果时遇到问题。
我收到错误日志:
yield result = {
^
SyntaxError: invalid syntax
当我删除“result =”时,我没有收到任何错误,但我这样做的原因是将结果保存为我在"f.write(result)"
中代码的最后一部分代码如下:
import scrapy
class ExampleSpider(scrapy.Spider):
name = "ufcspider"
start_urls = [
'http://quotes.toscrape.com/page/1/',
]
def parse(self, response):
for quote in response.css('div.quote'):
yield result = {
'text': quote.css('span.text::text').get(),
'author': quote.css('small.author::text').get(),
'link': 'http://quotes.toscrape.com' + quote.css("span a::attr(href)").get(),
'tags': quote.css('div.tags a.tag::text').getall(),
}
next_page = response.css("li.next a::attr(href)").get()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callable=self.parse)
page = response.url.split("/")[-2]
filename = f'quotes-{page}.json'
with open(filename, 'wb') as f:
f.write(result)
self.log(f'Saved file {filename}')
先定义result
,然后定义yield
result = { ... }
yield result