-2
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)
在本演示以上,urls
手動寫在start_requests()
方法,如果有50頁,網址是這樣的:
http://www.example.com/page/1
http://www.example.com/page/2
...
...
http://www.example.com/page/50
如何使這些URL用for循環?