-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwechatAPI.py
41 lines (39 loc) · 1.51 KB
/
wechatAPI.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
import requests
import json
import csv
def getGZHJson(appid, secret):
path = " https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential"
url = path + "&appid=" + appid + "&secret=" + secret
result = requests.get(url)
token = json.loads(result.text)
access_token = token['access_token']
data = {
"type": "news",
"offset": 0,
"count": 1,
}
headers = {
'content-type': "application/json",
'Accept-Language': 'zh-CN,zh;q=0.9'
}
url = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=' + access_token
result = requests.post(url=url, data=json.dumps(data), headers=headers)
result.encoding = result.apparent_encoding
result = json.loads(result.text)
count = int(result['total_count'])
gzh_dict = {"news_item": []}
for i in range(0, count):
data['offset'] = i
result = requests.post(url=url, data=json.dumps(data), headers=headers)
result.encoding = result.apparent_encoding
result = json.loads(result.text)
for item in result['item'][0]['content']['news_item']:
temp_dict = {}
temp_dict['title'] = item["title"]
temp_dict['digest'] = item["digest"]
temp_dict['url'] = item["url"]
temp_dict['thumb_url'] = item["thumb_url"]
print(temp_dict)
gzh_dict['news_item'].append(temp_dict)
return json.dumps(gzh_dict)
print(getGZHJson('APPID', 'SECRET'))