Cookies Python Requests [不获取 cookies]

huangapple go评论99阅读模式
英文:

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 = {
  &#39;Accept&#39;:&#39;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&#39;,
  &#39;Accept-Encoding&#39;:&#39;gzip, deflate, br&#39;,
  &#39;Accept-Language&#39;:&#39;ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7&#39;,
  &#39;Cache-Control&#39;:&#39;max-age=0&#39;,
  &#39;Content-Length&#39;:&#39;58&#39;,
  &#39;Content-Type&#39;:&#39;application/x-www-form-urlencoded&#39;,
  &#39;Cookie&#39;:&#39;__SecureToken=1234&#39;,
  &#39;Origin&#39;:&#39;https://setcookie.net&#39;,
  &#39;Referer&#39;:&#39;https://setcookie.net/&#39;,
  &#39;Sec-Ch-Ua&#39;:&#39;&quot;Not/A)Brand&quot;;v=&quot;99&quot;, &quot;Google Chrome&quot;;v=&quot;115&quot;, &quot;Chromium&quot;;v=&quot;115&quot;&#39;,
  &#39;Sec-Ch-Ua-Mobile&#39;:&#39;?0&#39;,
  &#39;Sec-Ch-Ua-Platform&#39;:&#39;&quot;Windows&quot;&#39;,
  &#39;Sec-Fetch-Dest&#39;:&#39;document&#39;,
  &#39;Sec-Fetch-Mode&#39;:&#39;navigate&#39;,
  &#39;Sec-Fetch-Site&#39;:&#39;same-origin&#39;,
  &#39;Sec-Fetch-User&#39;:&#39;?1&#39;,
  &#39;Upgrade-Insecure-Requests&#39;:&#39;1&#39;,
  &#39;User-Agent&#39;:&#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36&#39;
}

payload = {
  &#39;name&#39;:&#39;__Secure-next-auth.session-token.0&#39;,
  &#39;value&#39;:&#39;please&#39;,
  &#39;path&#39;:&#39;/&#39;,
  &#39;dom&#39;:&#39;none&#39;,
  &#39;ss&#39;:&#39;notset&#39;,
  &#39;httponly&#39;:&#39;on&#39;
}

r = s.post(&#39;https://setcookie.net/&#39;,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 &lt;RequestsCookieJar[]&gt;

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(&#39;https://setcookie.net/&#39;, data=payload, headers=headers)

Now, it prints:

&lt;RequestsCookieJar(&lt;Cookie help=please for setcookie.net/&gt;]&gt;

Also, you don't need to manually set headers for this to work. requests will set those automatically.

huangapple
  • 本文由 发表于 2023年8月9日 06:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76863581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定