import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_requests(self):
urls = [
'http://quotes.toscrape.com/page/1/',
'http://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 = "quotes-%s.html" % page
with open(filename, "wb") as f:
f.write(response.body)
self.log("Saved file %s" % filename)
答案 0 :(得分:1)
欢迎堆栈溢出Sidharth Jain。将来,请不要仅在问题中单独发布代码,还应包括所面临问题的摘要以及您试图解决的问题。
在大多数情况下,尤其是这种情况,包括执行日志也将很有帮助。
您的代码没有问题,我刚刚执行了它,并且文件C:\Program Files (x86)\MySQL\MySQL Connector C 6.0.2\include
和quotes-1.html
都是用预期的数据创建的。
您的执行过程中可能会出现类似以下内容的行:
quotes-2.html
这只是一个有用的日志,它会在启动Spider时显示,并且如果Spider运行时间较长,您会看到它每分钟更新一次。由于在这种情况下,蜘蛛会在不到一秒钟的时间内完成,因此您将看不到任何更新。
在这种情况下,更重要的是确保您具有以下几行:(假设您的2020-10-02 18:28:32 [scrapy.extensions.logstats] INFO: Crawled 0 pages (at 0 pages/min), scraped 0 items (at 0 items/min)
是LOG_LEVEL
)
DEBUG
如果不这样做,那么下一步就是确保您运行的蜘蛛正确。如果仍然不能解决问题,请在问题中包含执行日志。