我正在开始使用Scrapy,我想尝试一些教程来使用Scrapy创建蜘蛛。
到目前为止,这是我的代码:
import scrapy
class QuotesSpider(scrapy.Spider):
name = "quotes"
def start_request(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)
作为报告,我得到以下信息:
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] >>> import scrapy >>> self.log('saved file %s' % filename) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'self' is not defined
我对此很陌生,不知道该如何解决。希望您能够帮助我。 :)
答案 0 :(得分:1)
您应该在anaconda的项目目录中使用scrapy crawl quotes
来启动蜘蛛
答案 1 :(得分:0)
您在start_request s ()中有错字:
def start_requests(self):