非凡 发表于 2021-10-22 16:01:34

selenium 登录淘宝

想用selenium,登录淘宝
from selenium import webdriver

driver = webdriver.Edge()
# driver.implicitly_wait(10)


cookies = [
{
    "domain": ".taobao.com",
    "expirationDate": 1666374423.515151,
    "hostOnly": False,
    "httpOnly": False,
    "name": "_cc_",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "Vq8l%2BKCLiw%3D%3D",
    "id": 1
},
{
    "domain": ".taobao.com",
    "expirationDate": 1635487943.954798,
    "hostOnly": False,
    "httpOnly": False,
    "name": "_m_h5_tk",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "6a278ac9c6a52d56439ef163da_1634890701781",
    "id": 2
},
{
    "domain": ".taobao.com",
    "expirationDate": 1635487943.955119,
    "hostOnly": False,
    "httpOnly": False,
    "name": "_m_h5_tk_enc",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "af780e7ca2cdf048c8145d1ea0bba57f",
    "id": 3
},
{
    "domain": ".taobao.com",
    "hostOnly": False,
    "httpOnly": False,
    "name": "_tb_token_",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": True,
    "storeId": "0",
    "value": "3ae68db739715",
    "id": 4
},
{
    "domain": ".taobao.com",
    "expirationDate": 2187576996,
    "hostOnly": False,
    "httpOnly": False,
    "name": "cna",
    "path": "/",
    "sameSite": "unspecified",
    "secure": False,
    "session": False,
    "storeId": "0",
    "value": "GPRJFT3z+2MCAW8WbAj+3Oi7",
    "id": 5
},
{
    "domain": ".taobao.com",
    "hostOnly": False,
    "httpOnly": True,
    "name": "cookie2",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": True,
    "storeId": "0",
    "value": "1e14f31eba09326325706675f9e1353f",
    "id": 6
},
{
    "domain": ".taobao.com",
    "expirationDate": 1933168730.881554,
    "hostOnly": False,
    "httpOnly": True,
    "name": "enc",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "7EjJYwm5XAqHDMUgkts%2FBvUrdV4vVRLHAv4gBYjrF%2F2xC7DG7dqjUmY9png%3D%3D",
    "id": 7
},
{
    "domain": ".taobao.com",
    "expirationDate": 1649373531.22886,
    "hostOnly": False,
    "httpOnly": False,
    "name": "hng",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "CN%7Czh-CN%7CCNY%7C156",
    "id": 8
},
{
    "domain": ".taobao.com",
    "expirationDate": 1650435142,
    "hostOnly": False,
    "httpOnly": False,
    "name": "isg",
    "path": "/",
    "sameSite": "unspecified",
    "secure": False,
    "session": False,
    "storeId": "0",
    "value": "BKKiEVGIN4_eKypM0fAiAzgE86YNluKCf-w7ypXAv0I51IGeHSm96vtDlR6l",
    "id": 9
},
{
    "domain": ".taobao.com",
    "expirationDate": 1650361700,
    "hostOnly": False,
    "httpOnly": False,
    "name": "l",
    "path": "/",
    "sameSite": "unspecified",
    "secure": False,
    "session": False,
    "storeId": "0",
    "value": "eBxFY5N7jHscdWG-BO5Cnurza77O6QAb4lca6aCOFtmVNCLlqve5dtjgtCApetPogugcRLHR3V8wxDDBAqfjCurExvO.",
    "id": 10
},
{
    "domain": ".taobao.com",
    "expirationDate": 1637430423.513957,
    "hostOnly": False,
    "httpOnly": False,
    "name": "lgc",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "%5Cu6E29%5Cu5BA4%5Cu91CCde%5Cu4ED9%5Cu4EBA%5Cu638C",
    "id": 11
},
{
    "domain": ".taobao.com",
    "expirationDate": 1704187328.400969,
    "hostOnly": False,
    "httpOnly": False,
    "name": "miid",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "11479347556132136",
    "id": 12
},
{
    "domain": ".taobao.com",
    "expirationDate": 1635516744.599087,
    "hostOnly": False,
    "httpOnly": False,
    "name": "mt",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "ci=-1_0",
    "id": 13
},
{
    "domain": ".taobao.com",
    "expirationDate": 1666374423.513259,
    "hostOnly": False,
    "httpOnly": True,
    "name": "sgcookie",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "E100UX%2BnqwtYIOP5Sdp5KUnKSqTVAkq6sfIuSzmVm90XKQWjDXL6JRbJ66u6n8CPVZN0X%2Bse1JTsTHrQigKwvn0YzRxrD%2Fwk%3D",
    "id": 14
},
{
    "domain": ".taobao.com",
    "expirationDate": 1642614423.514144,
    "hostOnly": False,
    "httpOnly": False,
    "name": "t",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "a2a5ea195bde226a973fabdddc97e011",
    "id": 15
},
{
    "domain": ".taobao.com",
    "expirationDate": 1650361701,
    "hostOnly": False,
    "httpOnly": False,
    "name": "tfstk",
    "path": "/",
    "sameSite": "unspecified",
    "secure": False,
    "session": False,
    "storeId": "0",
    "value": "c5rGB0942PusAS03PGi_dsCVZorQYuX_aVBiodASQV50De8y1v6ns_1wl",
    "id": 16
},
{
    "domain": ".taobao.com",
    "expirationDate": 1648912731,
    "hostOnly": False,
    "httpOnly": False,
    "name": "thw",
    "path": "/",
    "sameSite": "unspecified",
    "secure": False,
    "session": False,
    "storeId": "0",
    "value": "cn",
    "id": 17
},
{
    "domain": ".taobao.com",
    "expirationDate": 1666374423.514926,
    "hostOnly": False,
    "httpOnly": False,
    "name": "tracknick",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "%5Cu6E29%5Cu5BA4%5Cu91CCde%5Cu4ED9%5Cu4EBA%5Cu638C",
    "id": 18
},
{
    "domain": ".taobao.com",
    "hostOnly": False,
    "httpOnly": False,
    "name": "uc1",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": True,
    "storeId": "0",
    "value": "cookie14=Uoe3c9QGLQcXKA%3D%3D",
    "id": 19
},
{
    "domain": ".taobao.com",
    "expirationDate": 1637430423.513768,
    "hostOnly": False,
    "httpOnly": True,
    "name": "uc3",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "lg2=UIHiLt3xD8xYTw%3D%3D&nk2DGQ9lSIbKo%3D&id2=W8rqIoC98nEw&vt3=F8dCujSjWoD%2BKRxP0so%3D",
    "id": 20
},
{
    "domain": ".taobao.com",
    "expirationDate": 1637430423.514727,
    "hostOnly": False,
    "httpOnly": True,
    "name": "uc4",
    "path": "/",
    "sameSite": "no_restriction",
    "secure": True,
    "session": False,
    "storeId": "0",
    "value": "id4=0%40WengePl7lmYPDHVt09D%3D&nk4=0%40r6qdi3JcUv9QGqFTuVvbYqNVqYOm%2F9PabA%3D%3D",
    "id": 21
}
]

driver.get('https://www.taobao.com/')
for item in cookies:
    driver.add_cookie(item)

driver.refresh()

我先用Edge浏览器获取下来的cookie后,用selenium登录淘宝,。
然后报错,这是什么问题?麻烦大神指导下
D:\Python\Python37\python.exe F:/pythonProject/zancun.py
Traceback (most recent call last):
File "F:/pythonProject/zancun.py", line 306, in <module>
    driver.add_cookie(item)
File "D:\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 1121, in add_cookie
    assert cookie_dict['sameSite'] in ['Strict', 'Lax', 'None']
AssertionError

进程已结束,退出代码为 1

suchocolate 发表于 2021-10-22 17:56:04

试试只用这几个key:
{'name': 'BA_HECTOR', 'value': '0p8k8k80012k8h0hcf1gn52fc0r', 'path': '/', 'domain': '.baidu.com', 'secure': False, 'httpOnly': False, 'expiry': 1634899963}

白two 发表于 2021-10-22 22:56:22

直接用的selenium抓取的cookie吗?
应该要做一些处理才能用

kogawananari 发表于 2021-10-23 13:48:33

你先用selenium进淘宝用input卡住,然后登陆了再生成cookie的json文件

用这个文件的时候 那个domain是小数点开头的 要把小数点去掉 (直接删掉domain这个键更好 我是这样做的)

    driver.get(domain)
    with open(cookies_json_path, 'r') as cookies_json:
      for cookie in json.loads(cookies_json.read()):
            del cookie['domain']
            driver.add_cookie(cookie)

非凡 发表于 2021-10-23 16:35:43

白two 发表于 2021-10-22 22:56
直接用的selenium抓取的cookie吗?
应该要做一些处理才能用

用的是Cookie Editor

非凡 发表于 2021-10-23 16:45:58

kogawananari 发表于 2021-10-23 13:48
你先用selenium进淘宝用input卡住,然后登陆了再生成cookie的json文件

用这个文件的时候 那个domain是小 ...

1、你这是说通过手动登录后,获取到cookie文件‘cookies_json_path’吗?

非凡 发表于 2021-10-23 16:46:27

suchocolate 发表于 2021-10-22 17:56
试试只用这几个key:

是了,谢谢~~~

kogawananari 发表于 2021-10-23 16:54:15

非凡 发表于 2021-10-23 16:45
1、你这是说通过手动登录后,获取到cookie文件‘cookies_json_path’吗?

我说的是用selenium获取cookie

然后内容存json文件

和cookie文件是两码事那玩意是sqlite3文件

非凡 发表于 2021-10-23 17:16:17

kogawananari 发表于 2021-10-23 16:54
我说的是用selenium获取cookie

然后内容存json文件


哦哦,好的,明白了,谢谢
页: [1]
查看完整版本: selenium 登录淘宝