我的意图是登录一个网站,然后从一个python脚本访问受保护的图像。
这是我现在所拥有的。
import requests s = requests.Session() s.get('* domain *') r_login = s.post('* domain * / user.php',headers = {'cmd':'login','loginname':'***','password':'***'}) print(s.cookies) print(r_login.status_code) r_img = s.get('* domain * / * protectedimage * .jpg') print(r_img.status_code) print(r.cookies) print(s.cookies ['PHPSESSID'])输出:
<< class'requests.cookies .RequestsCookieJar'> [< Cookie PHPSESSID = 664b0842085b847a04d415a22e013ad8 for * domain * />]> 200 403 << class'requests.cookies.RequestsCookieJar'> []> 664b0842085b847a04d415a22e013ad8我相信我可以成功登录,因为我已经下载了html文件,这样做后,它是以登录的形式。但我的问题是,在我看来,我的 PHPSESSID cookie不通过,所以我得到一个 403 错误。但我明显在我的会议中。我也尝试手动添加到我的r_img行,并没有什么区别,我仍然得到一个空 CookieJar 和 403 错误。这是不可能与只有请求模块?我忽略了什么吗?对不太熟悉 HTTP 请求,请原谅。
我使用Python 3.4只是为了清楚。
解决方案您将表单数据作为 HTTP标头传递。 POST登录表单应该发送表单元素为 data 参数:
r_login = s.post('* domain * / user.php', data = {'cmd':'login','loginname':'***','password':'*** '})检查返回的正文,而不仅仅是状态代码。您的POST请求已被服务器接受( 200 OK ),但由于未发布任何登录信息,因此正文很可能会告诉您登录错误,请重试。
服务器最有可能清除cookie再次看到,因为它是一个无效的登录会话,当您请求图像。 403响应可能包含 Set-Cookie 头,用于 PHPSESSID ,其中包含过去的日期以清除它。 p>
My intention is to log into a site and then access a protected image from a python script. I have both legal and working access from a browser.
This is what I have now.
import requests s = requests.Session() s.get('*domain*') r_login =s.post('*domain*/user.php', headers={'cmd': 'login', 'loginname': '***', 'password': '***' }) print (s.cookies) print (r_login.status_code) r_img = s.get('*domain*/*protectedimage*.jpg') print (r_img.status_code) print (r.cookies) print (s.cookies['PHPSESSID'])Output:
<<class 'requests.cookies.RequestsCookieJar'>[<Cookie PHPSESSID=664b0842085b847a04d415a22e013ad8 for *domain*/>]> 200 403 <<class 'requests.cookies.RequestsCookieJar'>[]> 664b0842085b847a04d415a22e013ad8I am sure I can successfully log in, because I have once downloaded the html file after doing so, and it was in a form of being logged in. But my problem is that it seems to me that my PHPSESSID cookie does not pass so I get a 403 error back. But I clearly have it in my session. I have also tried adding the cookie manually to my "r_img" line, and it made no difference, I still get an empty CookieJar and a 403 error back. Would this be not possible with only the requests modul? Did I overlook something? Excuse me for being not quite familiar with HTTP requests.
I'm using Python 3.4 just for sake of clarity.
解决方案You are passing in your form data as HTTP headers. A POST login form should send form elements as the data parameter instead:
r_login = s.post('*domain*/user.php', data={'cmd': 'login', 'loginname': '***', 'password': '***' })Do inspect the returned body, not just the status code. Your POST request was accepted by the server (200 OK) but since no login information was posted, the body will most likely tell you something like "login incorrect, please try again".
The server most likely cleared the cookie again seeing as it was not a valid login session when you requested the image. The 403 response probably contains a Set-Cookie header for PHPSESSID with a date in the past to clear it.
更多推荐
Python:请求会话登录Cookie
发布评论