Python:尝试使用请求发布表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20759981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python : Trying to POST form using requests
提问by Captain_Meow_Meow
I'm trying to login a website for some scraping using Python and requests library, I am trying the following (which doesn't work):
我正在尝试使用 Python 和请求库登录网站进行一些抓取,我正在尝试以下操作(不起作用):
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'niceusername','password':'123456'}
In [12]: r = requests.post('https://admin.example.com/login.php',headers=headers,data=payload)
But nada, getting a redirect to the login page. Do I need to open a session? am I doing a wrong POST request, do I need to load the cookies? or does session does that automatically? I am lost here, some help and explanations are needed.
但是nada,重定向到登录页面。我需要打开会话吗?我是否在执行错误的 POST 请求,是否需要加载 cookie?还是会话会自动执行?我在这里迷路了,需要一些帮助和解释。
The website I'm trying to login is php, do I need to "capture the set-cookie and set the cookie header"? if so I have no idea how to do it. The webpage is a form with the following (if it helps): input :username' 'password' 'id':'myform', 'action':"login.php
我尝试登录的网站是 php,我是否需要“捕获 set-cookie 并设置 cookie 标头”?如果是这样,我不知道该怎么做。该网页是一个包含以下内容的表单(如果有帮助的话):输入 :username' 'password' 'id':'myform', 'action':"login.php
Some extra information, maybe you can see what I'm missing here..
一些额外的信息,也许你可以看到我在这里遗漏了什么..
In [13]: r.headers
Out[13]: CaseInsensitiveDict({'content-encoding': 'gzip', 'transfer-encoding': 'chunked',
'set-cookie': 'PHPSESSID=v233mnt4malhed55lrpc5bp8o1; path=/',
'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'vary': 'Accept-Encoding', 'server': 'nginx',
'connection': 'keep-alive', 'pragma': 'no-cache',
'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0',
'date': 'Tue, 24 Dec 2013 10:50:44 GMT', 'content-type': 'text/html'})
In [14]: r.cookies
Out[14]: <<class 'requests.cookies.RequestsCookieJar'>[Cookie(version=0, name='PHPSESSID',
value='v233mnt4malhed55lrpc5bp8o1', port=None, port_specified=False, domain='admin.example.com',
domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False,
expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]>
I would really appreciate the help, thanks!
我真的很感激你的帮助,谢谢!
update, with answer thanks to atupal:
更新,感谢 atupal 的回答:
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'usr','pass':'123'}
link = 'https://admin.example.com/login.php'
session = requests.Session()
resp = session.get(link,headers=headers)
# did this for first to get the cookies from the page, stored them with next line:
cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))
resp = session.post(link,headers=headers,data=payload,cookies =cookies)
#used firebug to check POST data, password, was actually 'pass', under 'net' in param.
#and to move forward from here after is:
session.get(link)
采纳答案by atupal
You can use the Sessionobject
您可以使用Session对象
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
payload = {'username':'niceusername','password':'123456'}
session = requests.Session()
session.post('https://admin.example.com/login.php',headers=headers,data=payload)
# the session instance holds the cookie. So use it to get/post later.
# e.g. session.get('https://example.com/profile')
回答by HoangYell
Send a POST request with content type = 'form-data':
发送内容类型 = 'form-data' 的 POST 请求:
import requests
files = {
'username': (None, 'myusername'),
'password': (None, 'mypassword'),
}
response = requests.post('https://example.com/abc', files=files)
回答by bdfariello
I was having problems here (i.e. sending form-data whilst uploading a file) until I used the following:
我在这里遇到了问题(即在上传文件时发送表单数据),直到我使用以下内容:
files = {'file': (filename, open(filepath, 'rb'), 'text/xml'),
'Content-Disposition': 'form-data; name="file"; filename="' + filename + '"',
'Content-Type': 'text/xml'}
That's the input that ended up working for me. In Chrome Dev Tools -> Network tab, I clicked the request I was interested in. In the Headers tab, there's a Form Data section, and it showed both the Content-Disposition and the Content-Type headers being set there.
这就是最终为我工作的输入。在 Chrome Dev Tools -> Network 选项卡中,我单击了我感兴趣的请求。在 Headers 选项卡中,有一个 Form Data 部分,它显示了在那里设置的 Content-Disposition 和 Content-Type 标头。
I did NOT need to set headers in the actual requests.post() command for this to succeed (including them actually caused it to fail)
我不需要在实际的 requests.post() 命令中设置标题才能成功(包括它们实际上导致它失败)

