问题描述
我正在尝试使用Python中的请求"模块发布一个登录网站的请求,但它实际上无法正常工作.我是新来的...所以我不知道是否应该制作我的用户名和密码cookie或我发现的某种类型的HTTP授权内容(??).
I am trying to post a request to log in to a website using the Requests module in Python but its not really working. I'm new to this...so I can't figure out if I should make my Username and Password cookies or some type of HTTP authorization thing I found (??).
from pyquery import PyQuery
import requests
url = 'http://www.locationary.com/home/index2.jsp'
所以现在,我认为我应该使用发布"和cookie....
So now, I think I'm supposed to use "post" and cookies....
ck = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}
r = requests.post(url, cookies=ck)
content = r.text
q = PyQuery(content)
title = q("title").text()
print title
我有种感觉,我在做饼干的事情做错了...我不知道.
I have a feeling that I'm doing the cookies thing wrong...I don't know.
如果未正确登录,则首页标题应显示在"Locationary.com"上;如果登录,则应为首页".
If it doesn't log in correctly, the title of the home page should come out to "Locationary.com" and if it does, it should be "Home Page."
如果您可以向我解释一些有关请求和cookie的事情,并帮助我解决这个问题,我将不胜感激. :D
If you could maybe explain a few things about requests and cookies to me and help me out with this, I would greatly appreciate it. :D
谢谢.
...它仍然还没有真正起作用.好的...所以这就是登录前主页HTML的内容:
...It still didn't really work yet. Okay...so this is what the home page HTML says before you log in:
</td><td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_email.gif"> </td>
<td><input class="Data_Entry_Field_Login" type="text" name="inUserName" id="inUserName" size="25"></td>
<td><img src="http://www.locationary.com/img/LocationaryImgs/icons/txt_password.gif"> </td>
<td><input class="Data_Entry_Field_Login" type="password" name="inUserPass" id="inUserPass"></td>
所以我认为我做对了,但是输出仍然是"Locationary.com"
So I think I'm doing it right, but the output is still "Locationary.com"
第二次
我希望能够长时间保持登录状态,并且每当我请求该域下的页面时,我都希望内容显示出来就像登录一样.
I want to be able to stay logged in for a long time and whenever I request a page under that domain, I want the content to show up as if I were logged in.
推荐答案
如果您想要的信息在页面上,则在登录后立即被定向到...
让我们改为调用ck
变量payload
,就像在 Python请求文档:
If the information you want is on the page you are directed to immediately after login...
Lets call your ck
variable payload
instead, like in the python-requests docs:
payload = {'inUserName': 'USERNAME/EMAIL', 'inUserPass': 'PASSWORD'}
url = 'http://www.locationary.com/home/index2.jsp'
requests.post(url, data=payload)
否则...
请参见下面的 https://stackoverflow.com/a/17633072/111362 .
这篇关于如何“登录";使用Python的“请求"模块访问网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!