如何“登录"访问使用 Python 的 Requests 模块的网站? [英] How to "log in" to a website using Python's Requests module?

查看:17
本文介绍了如何“登录"访问使用 Python 的 Requests 模块的网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 中的 Requests 模块发布登录网站的请求,但它并没有真正起作用.我是新手……所以我不知道我是否应该制作我的用户名和密码 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'

所以现在,我想我应该使用post"和cookies....

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-requests 文档:

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 的 Requests 模块的网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆