Request.post什么都不返回

时间:2015-03-20 14:51:31

标签: python post python-requests

我的脚本非常简单,应该对网页执行登录过程:

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

为什么req2.text(虽然req2.status_code返回200)是空的? 当我在html源代码中检查表单 action 属性时,我看到没有 jsessionid 的URL。为什么?我应该在request.post中发送我的凭据到哪个地址?

PS。 pageid 是动态生成的,所以这就是我使用BeautifulSoup从html表单获取此URL的原因。

2 个答案:

答案 0 :(得分:0)

我发现了问题并且它非常简单 - 在这种情况下,提交按钮值是强制性的。 所以我需要做的只是将按钮值添加到凭证字典。

答案 1 :(得分:0)

尝试编码值:

static void Main(string[] args)
{
    var b = new Base();
    ((IInterface)b).Write();

    var c = new Child();
    ((IInterface)c).Write();
}
public interface IInterface
{
    void Write();
}
public class Base : IInterface
{
    void IInterface.Write()
    {
        Console.WriteLine("Base");
    }
}
public class Child : Base, IInterface // hack is here. Re Implemented :/
{
    void IInterface.Write()
    {
        Console.WriteLine("Child");
    }
}

Outputs

    Base
    Child

我相信你能写得更好。我有同样的问题,这就是解决方案。