-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathweibo_fans.py
206 lines (167 loc) · 6.79 KB
/
weibo_fans.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = 'zhangslob'
__date__ = '18-6-5'
# code is far away from bugs with the god animal protecting
I love animals. They taste delicious.
┏┓ ┏┓
┏┛┻━━━┛┻┓
┃ ☃ ┃
┃ ┳┛ ┗┳ ┃
┃ ┻ ┃
┗━┓ ┏━┛
┃ ┗━━━┓
┃ 神兽保佑 ┣┓
┃ 永无BUG! ┏┛
┗┓┓┏━┳┓┏┛
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛
"""
import json
import scrapy
from ..items import WeiboItem
class WeiboSpider(scrapy.Spider):
name = 'weibo'
start_urls = ['https://m.weibo.cn/api/container/getIndex?containerid=1076031223178222'
'&page={}'.format(i) for i in range(1, 366)]
repost = 'https://m.weibo.cn/api/statuses/repostTimeline?id={}&page={}'
comment = 'https://m.weibo.cn/api/comments/show?id={}&page={}'
attitudes = 'https://m.weibo.cn/api/attitudes/show?id={}&page={}'
user = 'https://m.weibo.cn/api/container/getIndex?type=uid&value={}'
custom_settings = {
'CONCURRENT_REQUESTS': 64,
'DOWNLOAD_DELAY': 0,
'COOKIES_ENABLED': False,
'LOG_LEVEL': 'INFO',
'RETRY_TIMES': 15,
'USER_AGENT': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/55.0.2883.75 Safari/537.36 Maxthon/5.1.3.2000",
'DEFAULT_REQUEST_HEADERS': {
"HOST": "www.zhihu.com",
"Referer": "https://www.zhihu.com",
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)"
" Chrome/55.0.2883.75 Safari/537.36 Maxthon/5.1.3.2000"
},
'REDIS_HOST': '127.0.0.1',
'REDIS_PORT': '6379',
'REDIS_DB': '0',
'MONGO_URL': 'mongodb://127.0.0.1:27017',
'MONGO_DB': name,
'ITEM_PIPELINES': {
'weibo.pipelines.WeiboPipeline': 301,
},
'DOWNLOADER_MIDDLEWARES': {
# 'weibo.middlewares.ProxyMiddleware': 543,
},
}
def parse(self, response):
# 如果需要登录,就结束吧,这里并没有设置重新采集这个请求,可以写一个中间件来重试
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
for i in data['data']['cards']:
mid = i['mblog'].get('id')
# 去第一页查询总共有多少翻页,转发
first_page = self.repost.format(mid, 1)
yield scrapy.Request(first_page, callback=self.first_repost, meta={'mid': mid})
# 评论
comment = self.comment.format(mid, 1)
yield scrapy.Request(comment, callback=self.first_comment, meta={'mid': mid})
# 点赞
attitudes = self.attitudes.format(mid, 1)
yield scrapy.Request(attitudes, callback=self.first_attitudes, meta={'mid': mid})
# 处理转发
def first_repost(self, response):
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
total_number = data['data'].get('total_number')
total_page = int(total_number // 55)
mid = response.meta['mid']
for i in range(1, total_page):
url = self.repost.format(mid, i)
yield scrapy.Request(url, callback=self.repost_data)
def repost_data(self, response):
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
for i in data['data']['data']:
user = dict()
user['id'] = i['user']['id']
user['statuses_count'] = i['user']['statuses_count']
user['screen_name'] = i['user']['screen_name']
user['profile_url'] = i['user']['profile_url']
user['description'] = i['user']['description']
user['gender'] = i['user']['gender']
user['followers_count'] = i['user']['followers_count']
user['follow_count'] = i['user']['follow_count']
item = WeiboItem()
item.update(user)
yield item
# 处理评论
def first_comment(self, response):
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
total_number = data['data'].get('total_number')
total_page = int(total_number // 55)
mid = response.meta['mid']
for i in range(1, total_page):
url = self.comment.format(mid, i)
yield scrapy.Request(url, callback=self.comment_data)
def comment_data(self, response):
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
for i in data['data']['data']:
user_id = i['user']['id']
yield scrapy.Request(self.user.format(user_id), callback=self.user_data)
# 处理点赞
def first_attitudes(self, response):
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
total_number = data['data'].get('total_number')
total_page = int(total_number // 55)
mid = response.meta['mid']
for i in range(1, total_page):
url = self.attitudes.format(mid, i)
yield scrapy.Request(url, callback=self.attitudes_data)
def attitudes_data(self, response):
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
for i in data['data']['data']:
user_id = i['user']['id']
yield scrapy.Request(self.user.format(user_id), callback=self.user_data)
def user_data(self, response):
if 'passport' in response.url:
return
data = json.loads(response.text)
if data['ok'] != 1:
return
item = WeiboItem()
item['id'] = data['data']['userInfo']['id']
item['statuses_count'] = data['data']['userInfo']['statuses_count']
item['screen_name'] = data['data']['userInfo']['screen_name']
item['profile_url'] = data['data']['userInfo']['profile_url']
item['description'] = data['data']['userInfo']['description']
item['gender'] = data['data']['userInfo']['gender']
item['followers_count'] = data['data']['userInfo']['followers_count']
item['follow_count'] = data['data']['userInfo']['follow_count']
yield item