-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
152 lines (132 loc) · 4.86 KB
/
main.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
import requests
from fastapi import FastAPI
import json
import re
import random
import time
import os
from bs4 import BeautifulSoup
wechatCreeper = FastAPI()
url = 'https://mp.weixin.qq.com'
header = {
"HOST": "mp.weixin.qq.com",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
}
with open("app_msg_list.csv", "w",encoding='utf-8') as file:
file.write("文章标识符aid,标题title,链接url,时间time\n")
i = 0
with open('cookie.txt', 'r', encoding='utf-8') as f:
cookie = f.read()
cookies = json.loads(cookie)
def gettoken():
response = requests.get(url=url, cookies=cookies)
token = re.findall(r'token=(\d+)', str(response.url))[0]
return token
@wechatCreeper.get('/api/search/author/{username}')
def searchforfakeid(username):
query_id = {
'action': 'search_biz',
'token': gettoken(),
'lang': 'zh_CN',
'f': 'json',
'ajax': '1',
'random': random.random(),
'query': username,
'begin': '0',
'count': '5',
}
search_url = 'https://mp.weixin.qq.com/cgi-bin/searchbiz?'
search_response = requests.get(search_url, cookies=cookies, headers=header, params=query_id)
lists = search_response.json().get('list')[0]
fakeid = lists.get('fakeid')
if fakeid!='':
print('找到公众号'+fakeid)
return{'username': username,'id':fakeid}
@wechatCreeper.get('/api/passages/{username}/{page}')
def getpassage(username,page):
query_id_data = {
'token': gettoken(),
'lang': 'zh_CN',
'f': 'json',
'ajax': '1',
'random': random.random(),
'action': 'list_ex',
'begin': '0',
'count': '5',
'query': '',
'fakeid': searchforfakeid(username).get('id'),
'type': '9'
}
appmsg_url = 'https://mp.weixin.qq.com/cgi-bin/appmsg?'
appmsg_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)
print(appmsg_response.json())
max_num = appmsg_response.json().get('app_msg_cnt')
num = int(int(max_num) / 5)
if int(page) > num:
print('页数超出最大值')
return -1
query_id_data = {
'token': gettoken(),
'lang': 'zh_CN',
'f': 'json',
'ajax': '1',
'random': random.random(),
'action': 'list_ex',
'begin': '{}'.format(str(page*5)),
'count': '5',
'query': '',
'fakeid': searchforfakeid(username).get('id'),
'type': '9'
}
print('翻页###################', page)
query_fakeid_response = requests.get(appmsg_url, cookies=cookies, headers=header, params=query_id_data)
fakeid_list = query_fakeid_response.json().get('app_msg_list')
datalist=[]
for item in fakeid_list:
print(item.get('link'))
aid = item['aid']
title = item['title']
url = item['link']
create_time = item['create_time']
datas={'id': username+'/' + aid, 'title': title, "url":url,"time":create_time,"time_stamp":create_time}
datalist.append(datas)
#info = '"{}","{}","{}","{}"'.format(str(item["aid"]), item['title'], item['link'], str(item['create_time']))
with open(str(username) + '_' + str(page) + '.json', 'w', encoding='utf-8') as f:
json.dump(datalist, f, indent=2, sort_keys=True, ensure_ascii=False)
return {'ret': datalist}
#文章爬取,页数从0开始
@wechatCreeper.get('/api/passage/{username}/{id}')
#以上是一次性爬取所有文章的函数
def getcontent(id, username):
i=0
title=''
id=''
author=str(username)
time=''
content=''
url=''
while 1:
if not os.path.exists(str(username) + '_' + str(i) + '.json'):
break
with open(str(username)+'_'+str(i)+'.json','r',encoding='utf-8') as f:
articlelist=json.load(f)
for item in articlelist:
if item['id']==item['id']:
title=item['title']
url=item['url']
time=item['time']
i=i+1
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"
}
r = requests.get(str(url),cookies=cookies,headers=headers)
if r.status_code == 200:
text = r.text
soup = BeautifulSoup(text)
content += soup.find("meta")['content']
content += soup.get_text(strip=True)
print(content)
with open(str(id)+'.json','w',encoding='utf-8') as f:
articledata={'title':title,'author':author,'time':time,'id':id,'content':content}
json.dump(articledata, f, indent=2, sort_keys=True, ensure_ascii=False)
return {'ret': articledata}