我对Python很陌生,并且尝试使用scrapy来构建一个web刮刀。我遵循了以下URL中的教程:https://doc.scrapy.org/en/latest/intro/tutorial.html,就像教程一样,在中运行程序之后,我检查了我的当前文件目录,但是找不到任何HTML。
是做错了什么还是安装错了?
以下是代码
import scrapy
class QuotesSpider(scrapy.Spider):
name ="quotes"
def start_requests(self):
urls =[
'https://quotes.toscrape.com/page/1/'
'https://quotes.toscrape.com/page/2/'
]
for url in urls:
yield scrapy.Request(url=url, callback=self.parse)
def parse(self, response):
page = response.url.split("/")[-2]
filename = f'quotes-{page}.html'
with open(filename, 'wb') as f:
f.write(response.body)
self.log(f'Saved file {filename}') 发布于 2022-10-18 02:30:57
也许您自己键入了代码,而不是复制教程中的代码。您忽略了url后面的逗号,这会导致代码出现问题。
解决办法:
将代码的以下部分放入
urls = [
'https://quotes.toscrape.com/page/1/'
'https://quotes.toscrape.com/page/2/'
]代之以:
urls = [
'https://quotes.toscrape.com/page/1/',
'https://quotes.toscrape.com/page/2/'
]注:在需要url后用逗号分隔。

https://stackoverflow.com/questions/74103306
复制相似问题