import scrapy
class QuotesSpider(scrapy.Spider):
name=“books_spider”
def start_requests(self):
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:48.0) Gecko/20100101 Firefox/48.0'}
urls=[
"http://books.toscrape.com/”catalogue/page-1",
]
for url in urls:
yield scrapy.Request(url=url,callback=self.parse,headers=headers)
def parse(self,response):
with open('ans2.csv','w') as f:
page=response.url.split("/")[-2]
for q in response.css("article.product_pod"):
link=q.css("div.image_container a::attr(href)").get()
title= q.css("h3 a::attr(title)").get()
price=q.css("div.product_price p::text").get()
f.write('"{}","{}","{}"'.format(str(link),str(title),str(price))+"\n")
f.close()
next_page=response.css("li.next a::attr(href)").get()
if next_page is not None:
next_page=response.urljoin(next_page)
yield scrapy.Request(next_page,callback=self.parse,headers=headers)