-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotes_random.py
More file actions
32 lines (28 loc) · 1.13 KB
/
Copy pathquotes_random.py
File metadata and controls
32 lines (28 loc) · 1.13 KB
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
# -*- coding: utf-8 -*-
"""
难道 Ctrl + C 强行退出程序??
# yield response.follow(next_page, self.parse, dont_filter=True)
mongo数据库去重:
以text、author为依据。
db.quotestoscrape.aggregate([
{$group: {_id: {text: '$text', author: '$author'}, count: {$sum: 1}, dups: {$addToSet: '$_id'}}},
{$match: {count: {$gt: 1}}}
]).forEach(function(doc){
doc.dups.shift();
db.quotestoscrape.remove({_id: {$in: doc.dups}});
})
"""
import scrapy
class QuotesXpathSpider(scrapy.Spider):
name = 'quotes_random'
allowed_domains = ['quotes.toscrape.com']
start_urls = ['http://quotes.toscrape.com/random']
def parse(self, response):
for quote in response.xpath('//div[@class="quote"]'):
yield {
'text': quote.xpath('.//span[@class="text"]/text()').get(),
'author': quote.xpath('.//small[@class="author"]/text()').get(),
'tags': quote.xpath('.//div[@class="tags"]/a[@class="tag"]/text()').getall(),
}
next_page = 'http://quotes.toscrape.com/random'
yield response.follow(next_page, self.parse, dont_filter=True)