-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguokr.py
134 lines (105 loc) · 4.12 KB
/
guokr.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
# @Time: 2019/8/7 19:59
# @Author: yhw-miracle
# @Email: [email protected]
# @File: guokr.py
# @Software: PyCharm
"""
url: GET https://www.guokr.com/ask/highlight
问答list://ul[@class="ask-list-cp"]/li
下一页: https://www.guokr.com + //a[text()="下一页"]/@href
title: //ul[@class="ask-list-cp"]/li//h2/a/text()
title_url: //ul[@class="ask-list-cp"]/li//h2/a/@href
summary: //ul[@class="ask-list-cp"]/li//p[contains(@class, "summary")]/text()
tag: //ul[@class="ask-list-cp"]/li//p[@class="tags"]/a/text()
focus_nums: //ul[@class="ask-list-cp"]/li//p[contains(@class,"focus-nums")]/span/text()
answer_nums: //ul[@class="ask-list-cp"]/li//p[contains(@class,"answer-nums")]/span/text()
构造数据:
{
"from": "guokr",
"data": [
{
"title": "",
"title_url": "",
"summary": "",
"tag": ["",],
"focus_nums": "",
"answer_nums": ""
},
]
}
"""
import requests
from lxml import etree
import json
import time
class Guokrspider(object):
def __init__(self):
self.headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"
}
self.base_url = "https://www.guokr.com/ask/highlight"
def request_data(self, url):
"""
:param url:
:return:
"""
resposne = requests.get(url, headers = self.headers)
return resposne.content.decode("utf-8")
def parser_data(self, data, page_num):
"""
:param data:
:param page_num:
:return:
"""
etree_to_result = etree.HTML(data)
detail_list = etree_to_result.xpath('//ul[@class="ask-list-cp"]/li')
detail_page = list()
if len(detail_list) > 0:
for index, detail in enumerate(detail_list):
print(">>>正在爬取第 {} 页中第 {} 个数据...".format(page_num, index + 1))
detail_data = dict()
title = detail.xpath('.//h2/a/text()')
title_url = detail.xpath('.//h2/a/@href')
summary = detail.xpath('.//p[contains(@class, "summary")]/text()')
tag = detail.xpath('.//p[@class="tags"]/a/text()')
focus_nums = detail.xpath('.//p[contains(@class,"focus-nums")]/span/text()')
answer_nums = detail.xpath('.//p[contains(@class,"answer-nums")]/span/text()')
detail_data["title"] = title[0] if len(title) > 0 else None
detail_data["title_url"] = title_url[0] if len(title_url) > 0 else None
detail_data["summary"] = summary[0] if len(summary) > 0 else None
detail_data["tag"] = tag if len(tag) > 0 else None
detail_data["focus_nums"] = focus_nums[0] if len(focus_nums) > 0 else None
detail_data["answer_nums"] = answer_nums[0] if len(answer_nums) > 0 else None
detail_page.append(detail_data)
next_url = etree_to_result.xpath('//a[text()="下一页"]/@href')
next_url = "https://www.guokr.com" + next_url[0] if len(next_url) > 0 else None
return detail_page, next_url
def save_data(self, f = None, t = "temp.md"):
"""
:param data:
:return:
"""
for i in f:
with open(t, "a", encoding = "utf-8") as file:
file.write(json.dumps(i, ensure_ascii = False) + "\n")
def run(self):
"""
:return:
"""
next_url = self.base_url
page_num = 1
while True:
if next_url is None:
break
print("正在爬取第 {} 页数据...".format(page_num))
response_data = self.request_data(next_url)
print("--->" + str(len(response_data)))
if len(response_data) < 10000:
print(response_data)
detail_page, next_url = self.parser_data(response_data, page_num)
self.save_data(f = detail_page, t = "guokr.json")
page_num += 1
time.sleep(3)
if __name__ == '__main__':
Guokrspider().run()