在Python中以字节型数据发送cookie
我有一个具有以下格式的字典:
{
"cookie_name": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx",
"cookie_name2": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx",
...
}
我正在尝试使用请求
将请求发送给网站上的cookie数据。
这是我尝试的:
import requests
cookies = {...}
r = requests.post('https://linktowebsite.com', cookies=cookies)
但是,我遇到了一个错误:
return session.request(method=method, url=url, **kwargs)
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 573, in request
prep = self.prepare_request(req)
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 471, in prepare_request
cookies = cookiejar_from_dict(cookies)
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 537, in cookiejar_from_dict
cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 352, in set_cookie
and cookie.value.startswith('"')
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
我尝试使用UTF-8编码/解码以及转换为字符串,但没有任何效果。
任何帮助都将不胜感激
:示例cookie值: {'asp.net_sessionId':b'v10k \ xfe:\ xe0 \ x18 \ xad \ xad \ x08 \ xd3 \ xe0 \ xe0 \ x12 \ x12 \ xe7` \ xe7` \ xe7 \ xe7 \ x15lib?\ x15lib? '}
\ xb6 \ xa0 \ x03g \
def get_cookies(ff_cookies, col_names):
cj = http.cookiejar
con = sqlite3.connect(ff_cookies)
cur = con.cursor()
cur.execute("SELECT "+", ".join(col_names)+" FROM cookies") # gets data for SQLlite file
# iterates through all columns and adds it to list
cookieList = [item for item in cur.fetchall()]
cookies_dict = {}
for name in col_names:
cookies_dict[name] = []
# formats dictionary using above list
cookies_dict_keys = list(cookies_dict)
for cookie in cookieList:
for i in range(len(cookie)):
cookies_dict[cookies_dict_keys[i]].append(cookie[i])
return cookies_dict
xe1w \ xb0- \ xf3 \ x0bh \ x0bh \ xd0 \ xccoi7 \ x10 \ x10 \ xe7。\ xaa \ xbf 是文件的路径,col_names
是SQL文件中的列的名称,作为字符串列表,
此函数的返回值然后将其送入requests.post 函数
也必须注意,此SQL文件是在我的Edge浏览器文件中找到的:c:\ users \ username \ appData \ local \ local \ Microsoft \ Microsoft \ edge \ edge \ user deag \ user default \ default \ network \ cookies
I have a dictionary with the following format:
{
"cookie_name": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx",
"cookie_name2": b"xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx\xxx",
...
}
I am attempting to use requests
to send a request to the website with the cookie data.
Here's what I've tried:
import requests
cookies = {...}
r = requests.post('https://linktowebsite.com', cookies=cookies)
However, I'm met with an error instead:
return session.request(method=method, url=url, **kwargs)
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 573, in request
prep = self.prepare_request(req)
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 471, in prepare_request
cookies = cookiejar_from_dict(cookies)
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 537, in cookiejar_from_dict
cookiejar.set_cookie(create_cookie(name, cookie_dict[name]))
File "C:\Users\omaro\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\cookies.py", line 352, in set_cookie
and cookie.value.startswith('"')
TypeError: startswith first arg must be bytes or a tuple of bytes, not str
I've tried encoding/decoding with UTF-8 as well as converting to string but nothing has worked.
Any help would be greatly appreciated
Edit: sample cookie value:{'ASP.NET_SessionId': b'v10K\xfe:\xe0\x18\xad\x08\xd3\xe0\xe8\x12\xe7`\xe7\x15lib?\xd2\xe7oX\xb4jvT7\xd5\xad\xb6\xa0 \x03G\xe1W\xb0-\xf3\x0bh\xd0\xccOI7\x10\xe7.\xaa\xbf'}
Here's how to reproduce my dictionary:
def get_cookies(ff_cookies, col_names):
cj = http.cookiejar
con = sqlite3.connect(ff_cookies)
cur = con.cursor()
cur.execute("SELECT "+", ".join(col_names)+" FROM cookies") # gets data for SQLlite file
# iterates through all columns and adds it to list
cookieList = [item for item in cur.fetchall()]
cookies_dict = {}
for name in col_names:
cookies_dict[name] = []
# formats dictionary using above list
cookies_dict_keys = list(cookies_dict)
for cookie in cookieList:
for i in range(len(cookie)):
cookies_dict[cookies_dict_keys[i]].append(cookie[i])
return cookies_dict
Where ff_cookies
is the path to the file and col_names
are the names of the columns in the SQL file as a list of strings
The return value of this function is then fed into the requests.post
function
It is also important to note that this SQL file was found in my edge browser files: C:\Users\username\AppData\Local\Microsoft\Edge\User Data\Default\Network\Cookies
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎无论您从哪里得到cookie do do not do not do not do not do not to not to not to。您可以使用 binascii.hexlify.hexlify 将这些binary Data转换为将这些Binary Data转换为十六进制,然后对其进行解码,以便可以将其作为cookie值传递:
尝试以下代码:
输出,
但是,我鼓励您检查是否发现所有发现的cookie值是这种情况。使用此方法对它们进行解码,并查看结果值是否有意义(因为它们实际上不是十六进制的机会)。
Seems like wherever you got the cookies from do no not want you reading them. You can use binascii.hexlify to convert this binary data to hexadecimal, and then decode it so it can be passed as cookie value:
Try this code:
Output
However, I will encourage you to check whether this is the case for all cookie values you have found. Decode them using this method and see if the resulting values make sense (because of the off chance that they are not actually hexadecimals).