简体   繁体   English

为什么我不能使用Python“请求”库填写此表单?

[英]Why I can't fill this form using Python “requests” library?

I want to fill a sign-in form using requests library in Python. 我想使用Python中的requests库填写登录表单。 As you see below, the name of fields are username and password : 如下所示,字段名称为usernamepassword 在此处输入图片说明

So I wrote the below script: 所以我写了下面的脚本:

session = requests.session()
p = session.post("http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx", {'username':'myUsername','password':'myPassword'})
print 'headers', p.headers
print 'cookies', requests.utils.dict_from_cookiejar(session.cookies)
print 'html',  p.text

The problem is, when I run that, it returns the Sign-in page again! 问题是,当我运行该命令时,它将再次返回“登录”页面! What's wrong with my program? 我的程序怎么了?

Update : 更新:

I also tried to send the parameters as data, but nothing changed. 我也尝试将参数作为数据发送,但是没有任何改变。 ie the below code returns the same output also : 即下面的代码也返回相同的输出:

payload = {'username': 'myUsername', 'password': 'myPassword'}
r = requests.post("http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx", data=payload)
print(r.text)

Update2 : Update2:

I catch the packets using Burp Suit to see the difference : 我使用Burp Suit抓包以查看区别:

This it the packet that my browser sends : 这是我的浏览器发送的数据包:

POST /SubSystem/Portal/Index1/Login/Login2.aspx HTTP/1.1
Host: 192.168.94.1
Proxy-Connection: keep-alive
Content-Length: 134
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://192.168.94.1
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.89 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cookie: ASP.NET_SessionId=nntwluu5uhrldzuymsv333uc; CURRENT_LANGUAGE_2=fa

__VIEWSTATE=%2FwEPDwUKMTY5ODAzNzY1MmRkpL4TXtJfODqn0yxypIJaOzkl4YUWynT8urN%2BYYXM%2FnY%3D&Command=LOGIN&username=myUsername&password=myPassword

And this it the packet that the second Python script sends : 这是第二个Python脚本发送的数据包:

POST /SubSystem/Portal/Index1/Login/Login2.aspx HTTP/1.1
Host: 192.168.94.1
Content-Length: 31
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: python-requests/2.4.3 CPython/2.7.0 Windows/7
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded

username=myUsername&password=myPassword

It looks like you firstly need to send a POST request, but from your recent update, that POST also expects some cookies available - there's also a hidden field called "Command" that needs to be sent with the value "LOGIN", so. 看来您首先需要发送POST请求,但是从您最近的更新来看,该POST还希望有一些cookie可用-还有一个名为“ Command”的隐藏字段,需要使用值“ LOGIN”发送。

import requests

URL = "http://192.168.94.1/SubSystem/Portal/Index1/Login/Login2.aspx"

# Set up session object so we can store cookies across requests
session = requests.session()
# GET the page to get any initial cookies required for the POST
r = session.get(URL)
# POST to the login form (making sure to add "Command")
r = session.post(URL, data={'Command': 'LOGIN', 'username': 'x', 'password': 'y'})

As to why you don't get images when you save the page is that when a browser loads a page, it sees links to resources/stylesheets/images and issues further requests to access them - all requests does is load the "text" of the page as it were. 至于为什么保存页面时没有得到图像,是因为浏览器加载页面时会看到指向资源/样式表/图像的链接,并发出进一步的访问requests -所有requests都加载了页面原样。 There's ways of achieving this, but that's out of scope as an answer to the core question here. 有实现此目的的方法,但这超出了这里核心问题的答案。


As per the comment regarding Can I do multiple logins using these session objects? 根据有关可以使用这些会话对象进行多次登录的注释 - here's one way of doing it... -这是一种实现方式...

# List of users and passwords
USERS = {
    'rod': 'password1',
    'jane': 'password2',
    'freddy': 'password3'
}
# Initial name->initial session with cookies
sessions = {name:requests.session().get(URL) for name in USERS}
# Login users
sessions.update(
    (name, session.post(URL, data={'Command': 'LOGIN', 'username': name, 'password': USERS[name]})) 
    for name, session in sessions.items()
)
# Now do something with users
r1 = sessions['rod'].get('http://something.com')
r2 = sessions['freddy'].get('http://something.com', params={'foo': 'bar'})
# etc...

Supporting @EsseTi's answer,Use data=Payload method to get them. 支持@EsseTi的答案,使用data = Payload方法获取它们。 .

import requests
payload = {'id_login': 'UserName', 'id_password': 'Password'}
url = #Your URL
something = requests.post(url, data=payload)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM