Python Selenium使用cookie实现自动登录微博

前言

模拟登录微博是实现微博网页爬虫的第一步,现在的微博网页版有个sina visit system,只有登录过后才能获取更多微博内容。本文使用selenium通过预登陆保存cookie到本地,之后重复登录只需要提取本地cookie即可免去每次扫码或者输密码登录。


一、预登陆获取cookie

1) cookie处理

先简单引入两个函数实现保存cookies及读取cookies:

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
import os
import json

class CookieLogin:
def __init__(self,f_path):
"""
对象初始化
:param url: 首页地址
:param f_path: Cookies文件保存路径
"""
# self.url = url
self.f_path = f_path
# self.browser = self.start_browser(executable_path)

def save_cookies(self, data, encoding="utf-8"):
"""
Cookies保存方法
:param data: 所保存数据
:param encoding: 文件编码,默认utf-8
"""
with open(self.f_path, "w", encoding=encoding) as f_w:
json.dump(data, f_w)
print("save done!")

def load_cookies(self, encoding="utf-8"):
"""
Cookies读取方法
:param encoding: 文件编码,默认utf-8
"""
if os.path.isfile(self.f_path):
with open(self.f_path, "r", encoding=encoding) as f_r:
user_cookies = json.load(f_r)
return user_cookies

2) 预登陆

用selenium模拟登录,人工扫描二维码转到登录页面后,使用**wd.get_cookies()**保存cookies

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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from time import sleep
from untitled.py爬虫项目.cookies_usage import CookieLogin

prefs = {
'profile.default_content_setting_values': {
'notifications': 2 # 隐藏chromedriver的通知
},
'credentials_enable_service': False, # 隐藏chromedriver自带的保存密码功能
'profile.password_manager_enabled': False # 隐藏chromedriver自带的保存密码功能
}

# 创建一个配置对象
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', prefs)
options.add_experimental_option('excludeSwitches', ['enable-automation']) # 设置为开发者模式,禁用chrome正受到自动化检测的显示
options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug

wd = webdriver.Chrome(service=Service(r'D:\Study\chromedriver.exe'), options=options)

# 最大化窗口
wd.maximize_window()
wd.implicitly_wait(5)

url = "https://weibo.com/"
wd.get(url=url)


# 现主页实现登录,用二维码扫就行
wd.find_element(By.XPATH, '//*[@id="__sidebar"]/div/div[1]/div[1]/div/button').click()
sleep(10)

# 保存cookie到本地
cookies = wd.get_cookies()

cookie_fname = 'cookie.json'
login = CookieLogin(cookie_fname)

login.save_cookies(cookies)

wd.close()
wd.quit()

二、登录测试

在这段测试中注意打开网页后一定要多给点睡眠时间,至少4s以上,保证能删除完没有登录时页面的cookies,再重新写入本地保存的登录后cookies。

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
url = "https://weibo.com/"
wd.get(url=url)

# cooikes = wd.get_cookies()
# for cooike in cooikes:
# print(cooike)
sleep(4)
wd.delete_all_cookies()

# 持久化登录,之后登录就不需要上面的扫二维码
login = CookieLogin("cookie.json")
cookies = login.load_cookies()
try:
for cookie in cookies:
cookie_dict = {
'domain': '.weibo.com',
'name': cookie.get('name'),
'value': cookie.get('value'),
"expires": '',
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False
}
print(cookie_dict)
wd.add_cookie(cookie_dict)
except Exception as e:
print(e)

sleep(5)

wd.refresh()

# cooikes2 = wd.get_cookies()
# for cooike in cooikes2:
# print(cooike)

sleep(5)
# wd.get(url)
url = "https://s.weibo.com/weibo?q=%E6%96%B0%E5%86%A0%E7%96%AB%E6%83%85"
wd.get(url)

sleep(2)
# cooikes3 = wd.get_cookies()
# for cooike in cooikes2:
# print(cooike)

wd.close()
wd.quit()

三、批量删除

批量删除的思路大概就是:先点击个人头像查看自己的全部微博,然后找到第一条微博,选择进行删除操作,刷新,他就会出栈操作,再删除当前下的第一条微博,依次进行

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
# 处理每一条微博
#<i class="woo-font woo-font--angleDown morepop_action_bk3Fq" title="更多"></i>
first_weibo = driver.find_element(
By.XPATH,
'//i[@class="woo-font woo-font--angleDown morepop_action_bk3Fq"]')
first_weibo.click()
sleep(2)

# <div class="woo-box-flex woo-box-column" style="width: 100%;"><div class="woo-box-flex woo-box-justifyBetween"><div>转换为自己可见</div><!----><!----></div><div class="morepop_desc_a9Lfe"></div></div>
only_me = driver.find_element(
By.XPATH,
'//div[normalize-space()="删除"]')
only_me.click()
sleep(2)

# <button class="woo-button-main woo-button-flat woo-button-primary woo-button-m woo-button-round woo-dialog-btn"><span class="woo-button-wrap"><!----><!----><!----><span class="woo-button-content">确定</span></span></button>
confirm_btn = driver.find_element(
By.XPATH,
'//button[@class="woo-button-main woo-button-flat woo-button-primary woo-button-m woo-button-round woo-dialog-btn"]')
confirm_btn.click()
sleep(2)

# 或者使用鼠标模拟点击
# from selenium.webdriver.common.action_chains import ActionChains
# actions = ActionChains(driver)
# actions.move_to_element(confirm_btn).click().perform()

driver.refresh()

四、完整代码

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
# save cookie.py
import os
import json

class CookieLogin:
def __init__(self,f_path):
"""
对象初始化
:param url: 首页地址
:param f_path: Cookies文件保存路径
"""
# self.url = url
self.f_path = f_path
# self.browser = self.start_browser(executable_path)

def save_cookies(self, data, encoding="utf-8"):
"""
Cookies保存方法
:param data: 所保存数据
:param encoding: 文件编码,默认utf-8
"""
with open(self.f_path, "w", encoding=encoding) as f_w:
json.dump(data, f_w)
print("save done!")

def load_cookies(self, encoding="utf-8"):
"""
Cookies读取方法
:param encoding: 文件编码,默认utf-8
"""
if os.path.isfile(self.f_path):
with open(self.f_path, "r", encoding=encoding) as f_r:
user_cookies = json.load(f_r)
return user_cookies

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from time import sleep

prefs = {
'profile.default_content_setting_values': {
'notifications': 2 # 隐藏chromedriver的通知
},
'credentials_enable_service': False, # 隐藏chromedriver自带的保存密码功能
'profile.password_manager_enabled': False # 隐藏chromedriver自带的保存密码功能
}

# 创建一个配置对象
edge_options = Options()
edge_options.binary_location = "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"
driver_path = "/Users/yanghongli/Desktop/edgedriver_mac64_m1/msedgedriver"
service = Service(driver_path)
driver = webdriver.Edge(service=service, options=edge_options)

# 最大化窗口
driver.maximize_window()
driver.implicitly_wait(5)

url = "https://weibo.com/"
driver.get(url=url)


# 现主页实现登录,用二维码扫就行
# driver.find_element(By.XPATH, '//*[@id="__sidebar"]/div/div[1]/div[1]/div/button').click()
sleep(10)

# 保存cookie到本地
cookies = driver.get_cookies()

cookie_fname = 'cookie.json'
login = CookieLogin(cookie_fname)

login.save_cookies(cookies)

driver.close()
driver.quit()
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
# login&&delete.py
import os
import json

class CookieLogin:
def __init__(self,f_path):
"""
对象初始化
:param url: 首页地址
:param f_path: Cookies文件保存路径
"""
# self.url = url
self.f_path = f_path
# self.browser = self.start_browser(executable_path)

def save_cookies(self, data, encoding="utf-8"):
"""
Cookies保存方法
:param data: 所保存数据
:param encoding: 文件编码,默认utf-8
"""
with open(self.f_path, "w", encoding=encoding) as f_w:
json.dump(data, f_w)
print("save done!")

def load_cookies(self, encoding="utf-8"):
"""
Cookies读取方法
:param encoding: 文件编码,默认utf-8
"""
if os.path.isfile(self.f_path):
with open(self.f_path, "r", encoding=encoding) as f_r:
user_cookies = json.load(f_r)
return user_cookies

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.by import By
from time import sleep

prefs = {
'profile.default_content_setting_values': {
'notifications': 2 # 隐藏chromedriver的通知
},
'credentials_enable_service': False, # 隐藏chromedriver自带的保存密码功能
'profile.password_manager_enabled': False # 隐藏chromedriver自带的保存密码功能
}

# 创建一个配置对象
edge_options = Options()
edge_options.binary_location = "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"
driver_path = "/Users/yanghongli/Desktop/edgedriver_mac64_m1/msedgedriver"
service = Service(driver_path)
driver = webdriver.Edge(service=service, options=edge_options)

# 最大化窗口
driver.maximize_window()
driver.implicitly_wait(5)

url = "https://weibo.com/"
driver.get(url=url)

# cooikes = wd.get_cookies()
# for cooike in cooikes:
# print(cooike)
sleep(4)
driver.delete_all_cookies()

# 持久化登录,之后登录就不需要上面的扫二维码
login = CookieLogin("cookie.json")
cookies = login.load_cookies()
try:
for cookie in cookies:
cookie_dict = {
'domain': '.weibo.com',
'name': cookie.get('name'),
'value': cookie.get('value'),
"expires": '',
'path': '/',
'httpOnly': False,
'HostOnly': False,
'Secure': False
}
print(cookie_dict)
driver.add_cookie(cookie_dict)
except Exception as e:
print(e)

sleep(5)

driver.refresh()

# cooikes2 = wd.get_cookies()
# for cooike in cooikes2:
# print(cooike)

sleep(5)
# wd.get(url)
# url = "https://s.weibo.com/weibo?q=labubu"
# driver.get(url)

# sleep(2)
# cooikes3 = wd.get_cookies()
# for cooike in cooikes2:
# print(cooike)

# <img src="https://tvax3.sinaimg.cn/crop.0.0.996.996.180/005AJQBUly8g68biwlhowj30ro0rotag.jpg?KID=imgbed,tva&amp;Expires=1751022479&amp;ssig=%2FIVjloIVc2" alt="profile" class="Ctrls_icon_2mxB4 Ctrls_avatar_3Hf0X">
profile = driver.find_element(
By.XPATH,
'//img[@alt="profile"]')
profile.click()
sleep(2)

# 获取所有微博的“更多”按钮(通过公共类名)
more_buttons = driver.find_elements(
By.XPATH,
'//i[@class="woo-font woo-font--angleDown morepop_action_bk3Fq"]'
)

print(f"找到 {len(more_buttons)} 条微博")



# 处理每一条微博
#<i class="woo-font woo-font--angleDown morepop_action_bk3Fq" title="更多"></i>
first_weibo = driver.find_element(
By.XPATH,
'//i[@class="woo-font woo-font--angleDown morepop_action_bk3Fq"]')
first_weibo.click()
sleep(2)

# # <div class="woo-box-flex woo-box-column" style="width: 100%;"><div class="woo-box-flex woo-box-justifyBetween"><div>转换为自己可见</div><!----><!----></div><div class="morepop_desc_a9Lfe"></div></div>
# only_me = driver.find_element(
# By.XPATH,
# '//div[normalize-space()="转换为自己可见"]')
# only_me.click()
# sleep(2)

# <button class="woo-button-main woo-button-flat woo-button-primary woo-button-m woo-button-round woo-dialog-btn"><span class="woo-button-wrap"><!----><!----><!----><span class="woo-button-content">确定</span></span></button>
only_me = driver.find_element(
By.XPATH,
'//div[normalize-space()="删除"]')
only_me.click()
sleep(2)

# <button class="woo-button-main woo-button-flat woo-button-primary woo-button-m woo-button-round woo-dialog-btn"><span class="woo-button-wrap"><!----><!----><!----><span class="woo-button-content">确定</span></span></button>
confirm_btn = driver.find_element(
By.XPATH,
'//button[@class="woo-button-main woo-button-flat woo-button-primary woo-button-m woo-button-round woo-dialog-btn"]')
confirm_btn.click()
sleep(2)

# from selenium.webdriver.common.action_chains import ActionChains
# actions = ActionChains(driver)
# actions.move_to_element(confirm_btn).click().perform()

driver.refresh()

sleep(5)
driver.close()
driver.quit()

参考链接


Python Selenium使用cookie实现自动登录微博
https://blog.baixf.shop/2025/06/27/selenium/Python Selenium使用cookie实现自动登录微博/
作者
白小飞
发布于
2025年6月27日
许可协议