使用插件:EditThisCookie

  1. 安装 Chrome 扩展:EditThisCookie 官方-Chrome 商店 第三方-极简插件
  2. 将扩展固定在右上角
  3. 进入 baidu.com—输入账号与密码登录—点击右上角的插件—点击导出按钮,即可将 cookie 复制到粘贴板
  4. 获得的 cookie 格式如下
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
[
{
"domain": ".baidu.com",
"expirationDate": 1703576842,
"hostOnly": false,
"httpOnly": false,
"name": "BA_HECTOR",
"path": "/",
"sameSite": "unspecified",
"secure": false,
"session": false,
"storeId": "1",
"value": "al2404008g258ka00080808h1ioicsb1q",
"id": 1
},
{
"domain": ".baidu.com",
"expirationDate": 1735026442.340862,
"hostOnly": false,
"httpOnly": false,
"name": "BAIDUID",
"path": "/",
"sameSite": "unspecified",
"secure": false,
"session": false,
"storeId": "1",
"value": "78C6164F8C0DAA8DEA1D11138163F1B8:FG=1",
"id": 2
},
{
"domain": ".baidu.com",
"expirationDate": 1735026442.814047,
"hostOnly": false,
"httpOnly": false,
"name": "BAIDUID_BFESS",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": "1",
"value": "78C6164F8C0DAA8DEA1D11138163F1B8:FG=1",
"id": 3
},
{
"domain": ".baidu.com",
"expirationDate": 1703576843.087224,
"hostOnly": false,
"httpOnly": false,
"name": "BDORZ",
"path": "/",
"sameSite": "unspecified",
"secure": false,
"session": false,
"storeId": "1",
"value": "B490B5EBF6F3CD402E515D22BCDA1598",
"id": 4
},
{
"domain": ".baidu.com",
"expirationDate": 1738050442.340607,
"hostOnly": false,
"httpOnly": false,
"name": "BIDUPSID",
"path": "/",
"sameSite": "unspecified",
"secure": false,
"session": false,
"storeId": "1",
"value": "78C6164F8C0DAA8DEA1D11138163F1B8",
"id": 5
},
{
"domain": ".baidu.com",
"expirationDate": 1735026442.340793,
"hostOnly": false,
"httpOnly": false,
"name": "H_PS_PSSID",
"path": "/",
"sameSite": "unspecified",
"secure": false,
"session": false,
"storeId": "1",
"value": "39839_39934_39936_39933_39943_39938_39999_39662_40009_40040",
"id": 6
},
{
"domain": ".baidu.com",
"expirationDate": 1738050442.340727,
"hostOnly": false,
"httpOnly": false,
"name": "PSTM",
"path": "/",
"sameSite": "unspecified",
"secure": false,
"session": false,
"storeId": "1",
"value": "1703490391",
"id": 7
},
{
"domain": ".baidu.com",
"expirationDate": 1735026442.865231,
"hostOnly": false,
"httpOnly": false,
"name": "ZFY",
"path": "/",
"sameSite": "no_restriction",
"secure": true,
"session": false,
"storeId": "1",
"value": ":AAaJdMlSOSkbpgXxY6e4GxuwZCSOQn4rLelzF6e6h1M:C",
"id": 8
},
{
"domain": "www.baidu.com",
"expirationDate": 1704354442,
"hostOnly": true,
"httpOnly": false,
"name": "BD_UPN",
"path": "/",
"sameSite": "unspecified",
"secure": false,
"session": false,
"storeId": "1",
"value": "12314753",
"id": 9
}
]

图1:获取cookie步骤

使用 python 脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import json
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By

service = Service(executable_path=r'chromedriver.exe')
driver = webdriver.Chrome(service=service)
driver.implicitly_wait(20)

cookie_save_path = "cookie_ddx.txt"
url = "https://www.baidu.com/"
driver.get(url)
input("手动登录后,按回车键")
data = driver.get_cookies()
with open(cookie_save_path, "w", encoding="utf-8") as fw:
json.dump(data, fw)
print(f"{cookie_save_path} 文件已经生成")

文件内容:

添加一些回车后: