-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthor.py
26 lines (21 loc) · 908 Bytes
/
author.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding: utf-8 -*-
import scrapy
class AuthorSpider(scrapy.Spider):
name = 'author'
start_urls = ['http://quotes.toscrape.com']
def parse(self, response):
# next page fetch detail
urls = response.css('div.quote > span > a::attr(href)').extract()
for url in urls:
url = response.urljoin(url)
yield scrapy.Request(url=url, callback=self.parse_detail)
# pagination
next_page_url = response.css('li.next > a::attr(href)').extract_first()
if next_page_url:
next_page_url = response.urljoin(next_page_url)
yield scrapy.Request(url=next_page_url, callback=self.parse)
def parse_detail(self, response):
yield{
'name':response.css('h3.author-title::text').extract_first(),
'birth_date':response.css('span.author-born-date::text').extract_first(),
}