英文:
Cookies Python Requests [don't get cookies]
问题
这是使用该网站https://setcookie.net/
的Python代码:
import requests
s = requests.Session()
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control':'max-age=0',
'Content-Length':'58',
'Content-Type':'application/x-www-form-urlencoded',
'Cookie':'__SecureToken=1234',
'Origin':'https://setcookie.net',
'Referer':'https://setcookie.net/',
'Sec-Ch-Ua':'"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
'Sec-Ch-Ua-Mobile':'?0',
'Sec-Ch-Ua-Platform':'"Windows"',
'Sec-Fetch-Dest':'document',
'Sec-Fetch-Mode':'navigate',
'Sec-Fetch-Site':'same-origin',
'Sec-Fetch-User':'?1',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}
payload = {
'name':'__Secure-next-auth.session-token.0',
'value':'please',
'path':'/',
'dom':'none',
'ss':'notset',
'httponly':'on'
}
r = s.post('https://setcookie.net/',json=payload,headers=headers)
print(s.cookies)
我只想获取cookies,但是我不知道我需要改变什么?
为了解决这个问题,我尝试使用headers
,并且需要完整的headers(你可以检查,但是一些数据可能会改变)
但是print(s.cookies)
也返回<RequestsCookieJar[]>
我不知道如何修复,但我可以说payload
不是Request Payload
,而是Form Data
。
英文:
It's the Python code to use this site: https://setcookie.net/
:
import requests
s = requests.Session()
headers = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
'Accept-Encoding':'gzip, deflate, br',
'Accept-Language':'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Cache-Control':'max-age=0',
'Content-Length':'58',
'Content-Type':'application/x-www-form-urlencoded',
'Cookie':'__SecureToken=1234',
'Origin':'https://setcookie.net',
'Referer':'https://setcookie.net/',
'Sec-Ch-Ua':'"Not/A)Brand";v="99", "Google Chrome";v="115", "Chromium";v="115"',
'Sec-Ch-Ua-Mobile':'?0',
'Sec-Ch-Ua-Platform':'"Windows"',
'Sec-Fetch-Dest':'document',
'Sec-Fetch-Mode':'navigate',
'Sec-Fetch-Site':'same-origin',
'Sec-Fetch-User':'?1',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36'
}
payload = {
'name':'__Secure-next-auth.session-token.0',
'value':'please',
'path':'/',
'dom':'none',
'ss':'notset',
'httponly':'on'
}
r = s.post('https://setcookie.net/',json=payload,headers=headers)
print(s.cookies)
I just wanna get cookies, but i can't what I must change?
So to fix the problem I tried use headers
and its full need headers (you can check, but some data maybe change)
But print(s.cookies)
also returns <RequestsCookieJar[]>
I don't know how to fix but I can say that payload
isn't Request Payload
it's Form Data
答案1
得分: 0
你的代码以JSON格式发送数据,而网站需要普通表单数据。
你应该修改s.post
,使用data=
而不是json=
,将格式从application/json
更改为application/x-www-form-urlencoded
。
r = s.post('https://setcookie.net/', data=payload, headers=headers)
现在,它会打印:
<RequestsCookieJar(<Cookie help=please for setcookie.net/>)>
此外,你不需要手动设置头部信息,requests
会自动设置。
英文:
Your code sends data in JSON format while the site looks for normal form data.
You should modify your s.post
to use data=
instead of json=
to change the format from application/json
to application/x-www-form-urlencoded
.
r = s.post('https://setcookie.net/', data=payload, headers=headers)
Now, it prints:
<RequestsCookieJar(<Cookie help=please for setcookie.net/>]>
Also, you don't need to manually set headers for this to work. requests
will set those automatically.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论