-
Notifications
You must be signed in to change notification settings - Fork 2
/
weibo_bak.py
194 lines (155 loc) · 5.59 KB
/
weibo_bak.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
import json
import re
import time
import requests
import fc
from loguru import logger
from lxml import etree
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
def get_driver():
options = webdriver.ChromeOptions()
fc.init_base_cap(options)
driver = webdriver.Chrome(options=options)
fc.add_stealth_js(driver)
return driver
# 10进制转为62进制
def base62_encode(num, alphabet=ALPHABET):
"""Encode a number in Base X
`num`: The number to encode
`alphabet`: The alphabet to use for encoding
"""
if (num == 0):
return alphabet[0]
arr = []
base = len(alphabet)
while num:
rem = num % base
num = num // base
arr.append(alphabet[rem])
arr.reverse()
return ''.join(arr)
# 62进制转为10进制
def base62_decode(string, alphabet=ALPHABET):
"""Decode a Base X encoded string into the number
Arguments:
- `string`: The encoded string
- `alphabet`: The alphabet to use for encoding
"""
base = len(alphabet)
strlen = len(string)
num = 0
idx = 0
for char in string:
power = (strlen - (idx + 1))
num += alphabet.index(char) * (base ** power)
idx += 1
return num
# mid转换为id
def mid2id(mid):
mid = str(mid)[::-1]
size = int(len(mid) / 7) if len(mid) % 7 == 0 else int(len(mid) / 7 + 1)
result = []
for i in range(size):
s = mid[i * 7: (i + 1) * 7][::-1]
s = base62_encode(int(s))
s_len = len(s)
if i < size - 1 and len(s) < 4:
s = '0' * (4 - s_len) + s
result.append(s)
result.reverse()
return ''.join(result)
# id转换为mid
def id2mid(id):
id = str(id)[::-1]
size = int(len(id) / 4) if len(id) % 4 == 0 else int(len(id) / 4 + 1)
result = []
for i in range(size):
s = id[i * 4: (i + 1) * 4][::-1]
s = str(base62_decode(str(s)))
s_len = len(s)
if i < size - 1 and s_len < 7:
s = (7 - s_len) * '0' + s
result.append(s)
result.reverse()
return ''.join(result)
class Weibo:
def __init__(self):
self.driver = get_driver()
def renew_cookie(self):
driver = get_driver()
def access_weibo(self):
try:
self.driver.get("https://weibo.com/")
# 等待页面加载
WebDriverWait(self.driver, 60).until(EC.presence_of_element_located((By.XPATH, "//input[@node-type='searchInput']")))
cookies = self.driver.get_cookies()
json_cookies = json.dumps(cookies)
with open('cookies.json', 'w', encoding='utf-8') as f:
f.write(json_cookies)
return True
except Exception as e:
print(e)
def scrape(self, url):
# 从微博爬数据
if re.search(r'(/status/\d{16})', url):
# 手机url需转换为PC端url - 手机端链接形式1
url = self.mobile2pc_url(url)
else:
match = re.search(r'/(\d{10})/(\d{16})', url)
if match:
# 手机url需转换为PC端url - 手机端链接形式1
uid = match.group(1) # 用户id
mid = mid2id(match.group(2))
url = f"https://www.weibo.com/{uid}/{mid}"
html = self.fetch_url(url)
if html == '':
return
self.extract(html)
def fetch_url(self, url):
# self.access_weibo()
with open('cookies.json', 'r', encoding='utf-8') as f:
list_cookies = json.loads(f.read())
cookie = '; '.join(item for item in [item["name"] + "=" + item["value"] for item in list_cookies])
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
'cookie': cookie
}
try:
r = requests.get(url, headers=headers, timeout=30)
if r.status_code != 200:
raise Exception(f'get {url} - {r.status_code}')
return r.text
except Exception as e:
logger.error("error when fetch", url, e)
return ''
def chrome_url(self, url):
self.driver.get(url)
# 等待页面加载
WebDriverWait(self.driver, 60).until(EC.presence_of_element_located((By.XPATH, "//input[@node-type='searchInput']")))
def extract(self, html):
selector = etree.HTML(html) # 将源码转化为能被XPath匹配的格式
data1 = re.findall(r'<strong class=\\"W_f16\\">(\d+)<\\/strong>', html)
fans = data1[1]
data2 = selector("//div[@class='WB_handle']//span[@class='line S_line1']")
share = data2[1].text
comments = data2[1].text
likes = data2[1].text
print(fans)
def mobile2pc_url(self, mweb_url):
"""手机 url 转电脑 url"""
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0'}
try:
mweb = requests.get(mweb_url, headers=headers, timeout=30)
if mweb.status_code != 200:
raise Exception(f'get {mweb_url} - {mweb.status_code}')
pc_id = re.search(r'"id": (\d+),', mweb.text).group(1)
pc_bid = re.search('"bid": "(.*?)",', mweb.text).group(1)
pc_url = f"https://www.weibo.com/{pc_id}/{pc_bid}"
return pc_url
except:
logger.error("error when mobileUrl 2 pcUrl")
return ''