-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotes_scroll.py
More file actions
29 lines (25 loc) · 862 Bytes
/
Copy pathquotes_scroll.py
File metadata and controls
29 lines (25 loc) · 862 Bytes
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
# -*- coding: utf-8 -*-
"""
http://quotes.toscrape.com/scroll
爬取动态加载的页面
"""
import json
import scrapy
class QuotesscrollSpider(scrapy.Spider):
name = 'quotes_scroll'
allowed_domains = ['quotes.toscrape.com']
page = 1
start_urls = ['http://quotes.toscrape.com/api/quotes?page=1']
def parse(self, response):
data = json.loads(response.text)
for quote in data["quotes"]:
yield {
"quote": quote["text"],
"author": quote["author"]["name"],
"goodreads_link": quote["author"]["goodreads_link"],
"tags": quote["tags"],
}
if data["has_next"]:
self.page += 1
url = "http://quotes.toscrape.com/api/quotes?page={}".format(self.page)
yield scrapy.Request(url=url, callback=self.parse)