-
Notifications
You must be signed in to change notification settings - Fork 0
/
quotes2_spider.py
43 lines (30 loc) · 1.11 KB
/
quotes2_spider.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
from __future__ import unicode_literals
import scrapy
#import os
#import string
import sys
reload(sys)
sys.setdefaultencoding('utf8')
class Quotes2Spider(scrapy.Spider):
name = "quotes2"
start_urls = [
'http://quotes.toscrape.com/page/1/'
]
def parse(self, response):
for quote in response.css('div.quote'):
# yield{
item = scrapy.Field()
item['text'] = quote.css('span.text::text').extract_first(),
item['author']= quote.css('small.author::text').extract_first(),
item['tag'] = quote.css('div.tags a.tag::text').extract(),
# }
# Removing unicode characters in spider itself by encoding to ascii.
# item['text'] = quote.css('span.text::text').extract_first().encode('ascii', 'ignore'),
# item['author']= quote.css('small.author::text').extract_first().encode('ascii', 'ignore'),
# item['tag'] = quote.css('div.tags a.tag::text').extract().encode('ascii', 'ignore'),
yield item
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)
#yield item