新建工程
scrapy startproject tutorial
进入tutorial目录,在spider下面新建quotes_spider.py
import scrapy
from ..items import QuotesItem
#coding:utf-8
class QuotesSpider(scrapy.Spider):
name = "quotes"
allowed_domain = "toscrape.com"
def start_requests(self):
for i in range(1,2):
url = "http://quotes.toscrape.com/page/" + str(i) + "/"
yield scrapy.Request(url=url,callback=self.parse)
def parse(self, response):
item = QuotesItem()
for quote in response.css('div.quote'):
item['text'] = quote.css('span.text::text').get(),
item['author'] = quote.css('small.author::text').get(),
item['tags'] = quote.css('div.tags a.tag::text').getall()
yield item