-
Notifications
You must be signed in to change notification settings - Fork 46
/
spider_3_quotes_authors.py
36 lines (31 loc) · 1.33 KB
/
spider_3_quotes_authors.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
27
28
29
30
31
32
33
34
35
36
import scrapy
class QuotesAuthorsSpider(scrapy.Spider):
name = 'quotes-authors'
start_urls = [
'http://quotes.toscrape.com/',
]
def parse(self, response):
for quote in response.css('div.quote'):
item = {
'text': quote.css('span.text::text').extract_first(),
'tags': quote.css('div.tags a.tag::text').extract(),
}
author_url = quote.css('.author + a::attr(href)').extract_first()
yield scrapy.Request(
response.urljoin(author_url),
meta={'item': item},
dont_filter=True,
callback=self.parse_author_page
)
next_page = response.css('li.next a::attr(href)').extract_first()
if next_page is not None:
next_page = response.urljoin(next_page)
yield scrapy.Request(next_page, callback=self.parse)
def parse_author_page(self, response):
item = response.meta.get('item', {})
item['author'] = {
'name': response.css('h3.author-title::text').extract_first(default='').strip(),
'birth_date': response.css('.author-born-date::text').extract_first(default='').strip(),
'birth_place': response.css('.author-born-location::text').extract_first(default='').strip(),
}
yield item