简体   繁体   English

Request.post不返回任何内容

[英]Request.post returns nothing

I have very simple script, which should perform login process to webpage: 我有一个非常简单的脚本,该脚本应该执行登录网页的过程:

import requests
from bs4 import BeautifulSoup

headers = {'User-Agent': 'Mozilla/5.0'}
credentials = {'user': 'user',
               'pass': 'pass'}
session = requests.Session()
req = session.get('https://myurl.com', verify=False)
html = BeautifulSoup(req.text)

login_form = html.find('form', {'name': 'loginForm'})
login_url = login_form['action']

# at this moment login_url has this form: https://myurl.com/webclient;jsessionid=AC7F87C5D38C0B2EABBF6D76379BB75B?pageid=816
req2 = session.post(login_url, data=credentials, headers=headers, verify=False)
print 'req2:', req2.text    # prints nothing

Why req2.text (although req2.status_code returns 200) is empty? 为什么req2.text(尽管req2.status_code返回200)为空? When I'm checking action atribute of form in html source I see URL without jsessionid . 当我在html源中检查表单的 动作属性时,我看到没有jsessionid的 URL。 Why? 为什么? And to which address should I send my credentials in request.post? 我应该将我的凭据在request.post中发送到哪个地址?

PS. PS。 pageid is generated dynamically, so this is the reason why I use BeautifulSoup to get this url from html form. pageid是动态生成的,因此这就是为什么我使用BeautifulSoup从html表单获取此url的原因。

I found the problem and it was really trivial - submit button value is mandatory in this case. 我发现了问题,这确实很简单-在这种情况下,提交按钮值是强制性的。 So all I need to do is just add button value to credentials dictionary. 因此,我要做的就是将按钮值添加到凭据字典中。

Try encoding the values: 尝试编码值:

credentials = {
      'user'.encode("utf-8"): 'user'.encode("utf-8"),
      'pass'.encode("utf-8"): 'pass'.encode("utf-8")
}

I trust you to write it better. 我相信你会写得更好。 I had the same problem and that was the solution. 我有同样的问题,那就是解决方案。

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

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