如何传递浏览器的硒cookie



我正在抓取一个驱动程序的cookie,将其存储在我的电子应用程序上作为一个临时字符串,并在bot启动时抓取它,唯一的问题是我无法通过driver.add_cookie()的cookie,因为它是一个字符串,有人知道我如何处理这个吗?

我当前的方法是:

box_of_cookies = "{'domain': '.kith.com', 'expiry': 1662256296, 'httpOnly': False, 'name': 'geolizr_geodata', 'path': '/', 'sameSite': 'Strict', 'secure': True, 'value': '{%22as%22:%22AS701%20MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22asname%22:%22UUNET%22%2C%22mobile%22:false%2C%22proxy%22:false%2C%22city%22:%22East%20Orange%22%2C%22currency%22:{%22code%22:%22USD%22}%2C%22country%22:{%22code%22:%22US%22%2C%22country%22:%22United%20States%22}%2C%22countryCode%22:%22US%22%2C%22continent%22:%22North%20America%22%2C%22continentCode%22:%22NA%22%2C%22isp%22:%22MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22lat%22:40.7523%2C%22lon%22:-74.2172%2C%22org%22:%22Verizon%20Communications%22%2C%22query%22:%2274.102.170.52%22%2C%22region%22:%22NJ%22%2C%22regionName%22:%22New%20Jersey%22%2C%22status%22:%22success%22%2C%22timezone%22:%22America/New_York%22%2C%22zip%22:%2207018%22%2C%22cloudflare%22:%22US%22%2C%22ttl%22:1129%2C%22env%22:%22PROD%22%2C%22version%22:%22ip-api%20cached%22%2C%22currencyCode%22:%22USD%22%2C%22countryName%22:%22United%20States%22%2C%22service%22:%22ip.lovely-app.com%22}'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_sa_t', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '2021-09-04T01%3A51%3A36.257Z'}, {'domain': 'kith.com', 'expiry': 1693792296, 'httpOnly': False, 'name': '__kla_id', 'path': '/', 'secure': False, 'value': 'eyIkcmVmZXJyZXIiOnsidHMiOjE2MzA3MjAyOTMsInZhbHVlIjoiIiwiZmlyc3RfcGFnZSI6Imh0dHBzOi8va2l0aC5jb20vYWNjb3VudC9sb2dpbiJ9LCIkbGFzdF9yZWZlcnJlciI6eyJ0cyI6MTYzMDcyMDI5NiwidmFsdWUiOiIiLCJmaXJzdF9wYWdlIjoiaHR0cHM6Ly9raXRoLmNvbS9hY2NvdW50L2xvZ2luIn19'}, {'domain': 'kith.com', 'expiry': 1662256295, 'httpOnly': True, 'name': 'secure_customer_sig', 'path': '/', 'secure': True, 'value': 'ab5c745f4c26465aaac384544b1f5648'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_sa_p', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': 'kith.com', 'expiry': 1631929894, 'httpOnly': True, 'name': 'cart_sig', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '90d14af23f488d1426f499a12b8cfd28'}, {'domain': 'kith.com', 'expiry': 1662256296, 'httpOnly': False, 'name': 'KL_FORMS_MODAL', 'path': '/', 'secure': False, 'value': '{%22disabledForms%22:{}%2C%22viewedForms%22:{%22TFUkCm%22:3408578}}'}, {'domain': '.kith.com', 'expiry': 1630720353, 'httpOnly': False, 'name': '_gat_gtag_UA_62344036_3', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1630806695, 'httpOnly': True, 'name': '_secure_session_id', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'ca70a6a5d3d396c11cc9a3513cb7cfb4'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '509e92eb-30fd-4db1-b8f4-be1c24db64bd'}, {'domain': 'kith.com', 'expiry': 1630723892, 'httpOnly': False, 'name': 'shopify_pay_redirect', 'path': '/', 'secure': False, 'value': 'pending'}, {'domain': '.kith.com', 'expiry': 1662256295, 'httpOnly': False, 'name': '_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '5bcc7bb2-1969-4a02-85bf-3d4a91fe7b1b'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_Full_Redirect', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'false'}, {'domain': '.kith.com', 'expiry': 1693792296, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.465106694.1630720293'}, {'domain': '.kith.com', 'expiry': 1630806696, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.703132267.1630720293'}, {'domain': 'kith.com', 'expiry': 1630722095, 'httpOnly': False, 'name': 'dynamic_checkout_shown_on_cart', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.kith.com', 'expiry': 1662256295, 'httpOnly': False, 'name': '_shopify_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '5bcc7bb2-1969-4a02-85bf-3d4a91fe7b1b'}, {'domain': 'kith.com', 'expiry': 1630722091, 'httpOnly': False, 'name': '_shopify_country', 'path': '/', 'secure': False, 'value': 'United+States'}, {'domain': '.kith.com', 'expiry': 1631929891, 'httpOnly': True, 'name': '_landing_page', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%2Faccount%2Flogin'}, {'domain': 'kith.com', 'httpOnly': False, 'name': 'snize-recommendation', 'path': '/', 'secure': False, 'value': 'syvcewxzszd'}, {'domain': '.kith.com', 'expiry': 1693792293, 'httpOnly': False, 'name': 'GlobalE_CT_Data', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%7B%22CUID%22%3A%22210526597.612443469.583%22%2C%22CHKCUID%22%3Anull%7D'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '509e92eb-30fd-4db1-b8f4-be1c24db64bd'}, {'domain': '.kith.com', 'expiry': 1631929891, 'httpOnly': True, 'name': '_orig_referrer', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': '.kith.com', 'expiry': 1662256297, 'httpOnly': False, 'name': '__zlcmid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '15ukIyQ66OqABfm'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_SupportThirdPartCookies', 'path': '/', 'secure': False, 'value': 'true'}, {'domain': '.kith.com', 'expiry': 1630979493, 'httpOnly': False, 'name': 'GlobalE_Data', 'path': '/', 'secure': False, 'value': '%7B%22countryISO%22%3A%22US%22%2C%22currencyCode%22%3A%22USD%22%2C%22cultureCode%22%3A%22en-US%22%7D'}, {'domain': '.kith.com', 'expiry': 1630720353, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1631929895, 'httpOnly': False, 'name': 'cart_currency', 'path': '/', 'secure': False, 'value': 'USD'}"

#adds cookie to driver
def parse_Cookie_string(driver,box_of_cookies):
#print(cookie.split("},"))
cookies_list = list(box_of_cookies.split("},"))
for cookie in cookies_list:
#check if its the last number element in the list if it is dont add the }
if(cookie == cookies_list[-1]):
pass
else:
cookie += "}" 
print(cookie)
driver.add_cookie(json.loads(cookie)

但我只是得到一个错误说->期望值:第1行第59列(char 58)

堆栈跟踪
Expecting value: line 1 column 59 (char 58)
During handling of the above exception, another exception occurred:
File    "/Users/raye/PycharmProjects/sneakerBots/kithbot.py", line 64, in parse_Cookie_string
driver.add_cookie(json.loads(cookie))
File "/Users/raye/PycharmProjects/sneakerBots/kithbot.py", line 213, in K_Step_Four
parse_Cookie_string(driver,MY_COOKIE)                   #|
File "/Users/raye/PycharmProjects/sneakerBots/kithbot.py", line 412, in START_KithBot
K_Step_Four(driver,email,fname,lname,address,city,zipcode,phoneNumber,account_email,account_password,COOKIE)

以上错误是driver.add_cookie()取字典的原因

def add_cookie(self, cookie_dict):
"""
Adds a cookie to your current session.
:Args:
- cookie_dict: A dictionary object, with required keys - "name" and "value";
optional keys - "path", "domain", "secure", "expiry"
Usage:
driver.add_cookie({'name' : 'foo', 'value' : 'bar'})
driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/'})
driver.add_cookie({'name' : 'foo', 'value' : 'bar', 'path' : '/', 'secure':True})
"""
self.execute(Command.ADD_COOKIE, {'cookie': cookie_dict})

所以基本上你有string,box_of_cookies需要先转换成dict然后才能通过:

如何转换:

import json
box_of_cookies = "{'domain': '.kith.com', 'expiry': 1662256296, 'httpOnly': False, 'name': 'geolizr_geodata', 'path': '/', 'sameSite': 'Strict', 'secure': True, 'value': '{%22as%22:%22AS701%20MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22asname%22:%22UUNET%22%2C%22mobile%22:false%2C%22proxy%22:false%2C%22city%22:%22East%20Orange%22%2C%22currency%22:{%22code%22:%22USD%22}%2C%22country%22:{%22code%22:%22US%22%2C%22country%22:%22United%20States%22}%2C%22countryCode%22:%22US%22%2C%22continent%22:%22North%20America%22%2C%22continentCode%22:%22NA%22%2C%22isp%22:%22MCI%20Communications%20Services%2C%20Inc.%20d/b/a%20Verizon%20Business%22%2C%22lat%22:40.7523%2C%22lon%22:-74.2172%2C%22org%22:%22Verizon%20Communications%22%2C%22query%22:%2274.102.170.52%22%2C%22region%22:%22NJ%22%2C%22regionName%22:%22New%20Jersey%22%2C%22status%22:%22success%22%2C%22timezone%22:%22America/New_York%22%2C%22zip%22:%2207018%22%2C%22cloudflare%22:%22US%22%2C%22ttl%22:1129%2C%22env%22:%22PROD%22%2C%22version%22:%22ip-api%20cached%22%2C%22currencyCode%22:%22USD%22%2C%22countryName%22:%22United%20States%22%2C%22service%22:%22ip.lovely-app.com%22}'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_sa_t', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '2021-09-04T01%3A51%3A36.257Z'}, {'domain': 'kith.com', 'expiry': 1693792296, 'httpOnly': False, 'name': '__kla_id', 'path': '/', 'secure': False, 'value': 'eyIkcmVmZXJyZXIiOnsidHMiOjE2MzA3MjAyOTMsInZhbHVlIjoiIiwiZmlyc3RfcGFnZSI6Imh0dHBzOi8va2l0aC5jb20vYWNjb3VudC9sb2dpbiJ9LCIkbGFzdF9yZWZlcnJlciI6eyJ0cyI6MTYzMDcyMDI5NiwidmFsdWUiOiIiLCJmaXJzdF9wYWdlIjoiaHR0cHM6Ly9raXRoLmNvbS9hY2NvdW50L2xvZ2luIn19'}, {'domain': 'kith.com', 'expiry': 1662256295, 'httpOnly': True, 'name': 'secure_customer_sig', 'path': '/', 'secure': True, 'value': 'ab5c745f4c26465aaac384544b1f5648'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_sa_p', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': 'kith.com', 'expiry': 1631929894, 'httpOnly': True, 'name': 'cart_sig', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '90d14af23f488d1426f499a12b8cfd28'}, {'domain': 'kith.com', 'expiry': 1662256296, 'httpOnly': False, 'name': 'KL_FORMS_MODAL', 'path': '/', 'secure': False, 'value': '{%22disabledForms%22:{}%2C%22viewedForms%22:{%22TFUkCm%22:3408578}}'}, {'domain': '.kith.com', 'expiry': 1630720353, 'httpOnly': False, 'name': '_gat_gtag_UA_62344036_3', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1630806695, 'httpOnly': True, 'name': '_secure_session_id', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'ca70a6a5d3d396c11cc9a3513cb7cfb4'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_shopify_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '509e92eb-30fd-4db1-b8f4-be1c24db64bd'}, {'domain': 'kith.com', 'expiry': 1630723892, 'httpOnly': False, 'name': 'shopify_pay_redirect', 'path': '/', 'secure': False, 'value': 'pending'}, {'domain': '.kith.com', 'expiry': 1662256295, 'httpOnly': False, 'name': '_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '5bcc7bb2-1969-4a02-85bf-3d4a91fe7b1b'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_Full_Redirect', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': 'false'}, {'domain': '.kith.com', 'expiry': 1693792296, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.465106694.1630720293'}, {'domain': '.kith.com', 'expiry': 1630806696, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.703132267.1630720293'}, {'domain': 'kith.com', 'expiry': 1630722095, 'httpOnly': False, 'name': 'dynamic_checkout_shown_on_cart', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.kith.com', 'expiry': 1662256295, 'httpOnly': False, 'name': '_shopify_y', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '5bcc7bb2-1969-4a02-85bf-3d4a91fe7b1b'}, {'domain': 'kith.com', 'expiry': 1630722091, 'httpOnly': False, 'name': '_shopify_country', 'path': '/', 'secure': False, 'value': 'United+States'}, {'domain': '.kith.com', 'expiry': 1631929891, 'httpOnly': True, 'name': '_landing_page', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%2Faccount%2Flogin'}, {'domain': 'kith.com', 'httpOnly': False, 'name': 'snize-recommendation', 'path': '/', 'secure': False, 'value': 'syvcewxzszd'}, {'domain': '.kith.com', 'expiry': 1693792293, 'httpOnly': False, 'name': 'GlobalE_CT_Data', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '%7B%22CUID%22%3A%22210526597.612443469.583%22%2C%22CHKCUID%22%3Anull%7D'}, {'domain': '.kith.com', 'expiry': 1630722096, 'httpOnly': False, 'name': '_s', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '509e92eb-30fd-4db1-b8f4-be1c24db64bd'}, {'domain': '.kith.com', 'expiry': 1631929891, 'httpOnly': True, 'name': '_orig_referrer', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': ''}, {'domain': '.kith.com', 'expiry': 1662256297, 'httpOnly': False, 'name': '__zlcmid', 'path': '/', 'sameSite': 'Lax', 'secure': False, 'value': '15ukIyQ66OqABfm'}, {'domain': '.kith.com', 'httpOnly': False, 'name': 'GlobalE_SupportThirdPartCookies', 'path': '/', 'secure': False, 'value': 'true'}, {'domain': '.kith.com', 'expiry': 1630979493, 'httpOnly': False, 'name': 'GlobalE_Data', 'path': '/', 'secure': False, 'value': '%7B%22countryISO%22%3A%22US%22%2C%22currencyCode%22%3A%22USD%22%2C%22cultureCode%22%3A%22en-US%22%7D'}, {'domain': '.kith.com', 'expiry': 1630720353, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'kith.com', 'expiry': 1631929895, 'httpOnly': False, 'name': 'cart_currency', 'path': '/', 'secure': False, 'value': 'USD'}"
box_of_cookies_replace = box_of_cookies.replace("{", "").replace("}", "").split(",")
box_of_cookies_as_dict = {}
for i in box_of_cookies_replace:
box_of_cookies_as_dict[i.split(":")[0].strip(''').replace(""", "")] = i.split(":")[1].strip('"'')

现在已成功转换为字典。您可以按如下方式添加。

driver.add_cookie(box_of_cookies_as_dict)

相关内容

  • 没有找到相关文章

最新更新