|
1 | 1 | import json |
| 2 | +import re |
2 | 3 |
|
3 | 4 | import scrapy |
4 | | -from twisted.web.xmlrpc import payloadTemplate |
5 | 5 |
|
6 | 6 |
|
7 | 7 | class ManningSpider(scrapy.Spider): |
8 | 8 | name = "manning" |
9 | 9 | allowed_domains = ['www.manning.com'] |
10 | 10 |
|
| 11 | + # 因为 headers 要给多个方法调用(如:start_requests(), parse()),所以声明未类变量 |
| 12 | + headers = { |
| 13 | + 'Content-Type': 'application/json', |
| 14 | + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' |
| 15 | + '(KHTML, like Gecko) Chrome/120.0 Safari/537.36', |
| 16 | + 'Accept': 'application/json, text/plain, */*', |
| 17 | + 'Origin': 'https://www.manning.com', |
| 18 | + 'Referer': 'https://www.manning.com/', |
| 19 | + } |
| 20 | + |
| 21 | + payload = { |
| 22 | + "accessType": [], |
| 23 | + # keywords: 书名里包含的关键字 |
| 24 | + "keywords": [], |
| 25 | + "level": [], |
| 26 | + "meapFilter": "published", |
| 27 | + "productType": [ |
| 28 | + "book" |
| 29 | + ], |
| 30 | + "programmingLanguages": [ |
| 31 | + "python" |
| 32 | + ], |
| 33 | + "selectedCategoryIds": [ |
| 34 | + 1 |
| 35 | + ], |
| 36 | + "sort": "newest", |
| 37 | + "includePrices": True |
| 38 | + } |
| 39 | + |
11 | 40 | def start_requests(self): |
12 | 41 | """ |
13 | 42 | 重写该方法以执行 POST 方法 |
14 | 43 | """ |
15 | | - headers = { |
16 | | - 'Content-Type': 'application/json', |
17 | | - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 ' |
18 | | - '(KHTML, like Gecko) Chrome/120.0 Safari/537.36', |
19 | | - 'Accept': 'application/json, text/plain, */*', |
20 | | - 'Origin': 'https://www.manning.com', |
21 | | - 'Referer': 'https://www.manning.com/', |
22 | | - } |
23 | | - payload = { |
24 | | - "accessType": [], |
25 | | - # keywords: 书名里包含的关键字 |
26 | | - "keywords": [], |
27 | | - "level": [], |
28 | | - "meapFilter": "published", |
29 | | - "productType": [ |
30 | | - "book" |
31 | | - ], |
32 | | - "programmingLanguages": [ |
33 | | - "python" |
34 | | - ], |
35 | | - "selectedCategoryIds": [ |
36 | | - 1 |
37 | | - ], |
38 | | - "sort": "newest", |
39 | | - "includePrices": True |
40 | | - } |
41 | 44 |
|
42 | 45 | yield scrapy.Request( |
43 | 46 | url='https://www.manning.com/search/getCatalogData', |
44 | 47 | method='POST', |
45 | | - headers=headers, |
46 | | - body=json.dumps(payload), |
47 | | - # cookies=cookies, |
| 48 | + headers=self.headers, |
| 49 | + body=json.dumps(self.payload), |
48 | 50 | callback=self.parse, |
49 | | - # meta={'page': self.start_page, 'payload': payload}, |
50 | 51 | ) |
51 | 52 |
|
| 53 | + def parse_detail(self, response): |
| 54 | + """ |
| 55 | + 从详情页解析 ISBN 和 description(可选) |
| 56 | + """ |
| 57 | + import time |
| 58 | + self.logger.info(f"parse_detail start {time.time()}") |
| 59 | + book = response.meta['book'] |
| 60 | + |
| 61 | + # 获取 html 页面的 ISBN |
| 62 | + isbn_text = response.xpath( |
| 63 | + '//div[contains(@class,"product-meta")]' |
| 64 | + '//li[starts-with(normalize-space(),"ISBN")]/text()' |
| 65 | + ).get() |
| 66 | + m = re.search(r'ISBN(?:-13)?:?\s*([0-9\-]{10,17})',isbn_text) |
| 67 | + if m: |
| 68 | + book['isbn'] = m.group(1).replace('-', '') |
| 69 | + |
| 70 | + book['isbn'] = book.get('isbn') or None |
| 71 | + |
| 72 | + yield book |
| 73 | + |
52 | 74 | def parse(self, response): |
| 75 | + """ |
| 76 | + response: 将执行 start_requests() 里面的 scrapy.Request()得到的响应 作为这里的 response |
| 77 | + """ |
53 | 78 | data = response.json() |
54 | 79 | books = data['products'] |
55 | 80 | for book in books: |
56 | | - item_data = { |
57 | | - 'title': book['title'], |
58 | | - 'author': book['authorshipDisplay'], |
59 | | - 'price': book['price'], |
60 | | - 'url': book['link'], |
61 | | - 'description': None, |
62 | | - 'publisher': 'manning', |
63 | | - } |
64 | | - yield item_data |
| 81 | + book['publisher'] = 'manning' |
| 82 | + yield scrapy.Request( |
| 83 | + url=book['link'], |
| 84 | + callback=self.parse_detail, |
| 85 | + meta={'book': book}, |
| 86 | + ) |
| 87 | + |
| 88 | + # 如果有下一页,且不是最后一页 |
| 89 | + # and 的优先级高于 :=,所以前半部分用括号括起来 |
| 90 | + if (pagination := data.get('pagination')) and pagination.get('hasNextPage'): |
| 91 | + payload = self.payload.copy() |
| 92 | + payload['page'] = pagination['page'] + 1 |
| 93 | + |
| 94 | + yield scrapy.Request( |
| 95 | + url='https://www.manning.com/search/getCatalogData', |
| 96 | + method='POST', |
| 97 | + headers=self.headers, |
| 98 | + body=json.dumps(payload), |
| 99 | + callback=self.parse, |
| 100 | + ) |
0 commit comments