【问题标题】:POST call with authentication to same URL, using Python Requests使用 Python 请求对同一 URL 进行身份验证的 POST 调用
【发布时间】:2016-08-23 22:14:51
【问题描述】:

我正在通过以下 URL 对脚本进行 POST 调用(我公司内部,因此无法从外部访问): https://opsdata.mycompany.com/scripts/finance/finance.exe

初始站点是一个 html 页面,其中包含供您输入数据的文本框,并且对上述 url 有一个 post 操作。但是,它会重定向到登录页面,该页面也位于上述 url,其中包含用户名和密码的文本框。我使用以下代码向登录页面提交数据:

post_url_finance = 'https://opsdata.*****.com/scripts/finance/finance.exe'
s = requests.session()
s.auth = {'USER_NAME': '*****', 'PASSWORD': '*****'}
proxies = {'http': 'http://proxy-***.****.com'}

要进行身份验证,我使用的是:

pageCert = requests.post(post_url_finance, proxies=proxies, verify=False)  

这给了我一个回应:

<Response [200]>
C:\Python27\lib\site-packages\urllib3\connectionpool.py:768: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
  InsecureRequestWarning)

但是,我需要发送我正在查询的数据以使用此信息:

values_finance = {'EMPLOYEE_TOTAL': '-----'}

当我第二次使用:

page = requests.post(post_url_finance, data=values_finance, proxies=proxies, verify=False) 

我得到了同样的回复。

<Response [200]>

如何进行第二次调用 Post 检索我想要的数据?

【问题讨论】:

    标签: python authentication post python-requests session-cookies


    【解决方案1】:

    all status_code=200 表示网站“成功呈现页面”很多时候网站可能不会使其无效登录页面或错误页面返回任何其他内容

    您需要查看pageCert.content ...。我认为您实际上并没有登录(也许您是)...在第二次通话中您需要这样做

    page = s.post(url,...)
    

    要获取您可能想要使用 json 的 api 数据

    page_data = s.post(url,...).json()
    

    【讨论】:

      【解决方案2】:

      我第一次看到这个东西,但文档给了我一些指导......看起来你只是打印出响应而不是数据,举个例子:

      r = requests.get('https://www.google.com')
      print(r)
      # <response 200>
      # Now if I write the text:
      print(r.text)
      # A lot of html comes out
      

      正如@Joran Beasley 所说,您可能只需要使用print(r.json) 来查看您需要什么。在理想情况下,获得 200 响应码是一件好事,否则如果身份验证失败,您将收到 401/403 错误。

      这个例外更多地与 mycompany.com 端的证书的真实性有关,您可以在 urllib3 文档中阅读。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-03
        • 2014-10-02
        • 1970-01-01
        • 1970-01-01
        • 2017-10-14
        • 1970-01-01
        • 2020-06-02
        相关资源
        最近更新 更多