This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
weibo.py
122 lines (96 loc) · 2.7 KB
/
weibo.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
# -*- coding: utf-8 -*-
import setting
import requests
import copy
import re
import sys
reload(sys)
sys.setdefaultencoding('utf8')
# 除去字符串中的html标签
def dr_to_dd(dr_str):
dr = re.compile(r'<[^>]+>', re.S)
dd = dr.sub('', dr_str)
return str(dd)
# global response
def init():
# 更改ajax_url
ajax_url = str(setting.weibo_url())
header = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
# 更改value&pro_id
form = {
'containerid': int(setting.weibo_id()),
}
response = requests.post(ajax_url, form, headers=header).json()
return response
def getdata(i):
response = copy.copy(init())
datas = response['data']['cards'][i]
return datas
def checkid(i):
datas = getdata(i)
return str(datas['mblog']['id'])
def checkretweet(i):
datas = getdata(i)
if datas['mblog'].get('retweeted_status') is None:
return False
else:
return True
def getweibo(i):
datas = getdata(i)
r_weibo = str(datas['mblog']['text'])
# 20170917增加
# 微博内容去除html标记功能
r2d_weibo = dr_to_dd(r_weibo)
return r2d_weibo
def getretweetweibo(i):
datas = getdata(i)
r_retweeetweibo = str(datas['mblog']['raw_text'])
# 20170917增加
# 微博内容去除html标记功能
r2d_retweeetweibo = dr_to_dd(r_retweeetweibo)
return r2d_retweeetweibo
def checkpic(i):
datas = getdata(i)
if datas['mblog'].get('pics') is None:
return False
else:
return True
def getpic(i):
datas = getdata(i)
picurl = ""
picnum = 1
for pic in datas['mblog']['pics']:
picurl = picurl + "微博配图" + str(picnum) + ":" + str(pic['url']) + '\n'
picnum += 1
return picurl
def getscheme(i):
datas = getdata(i)
return str(datas['scheme'])
def getidarray():
weibo_id_array = []
response = copy.copy(init())
cards = response['data']['cards']
for card in cards:
try:
weibo_id = card['mblog']['id']
except Exception as e:
weibo_id_array.append("0")
else:
weibo_id_array.append(weibo_id)
return weibo_id_array
# 20170926
# 现在查询新微博:返回前5个微博id(如果是微博广告位,id为0)
def get_5_idarray():
weibo_id_array = []
response = copy.copy(init())
cards = response['data']['cards']
for i in range(0, 5):
datas = cards[i]
try:
weibo_id = datas['mblog']['id']
except Exception as e:
weibo_id_array.append("0")
else:
weibo_id_array.append(weibo_id)
return weibo_id_array