【问题标题】:Python Requests authentication with proxy for POST callPython 请求使用代理进行身份验证以进行 POST 调用
【发布时间】:2016-04-22 22:52:01
【问题描述】:

我正在使用我公司的互联网,我需要访问一个网页以从其中抓取数据。我正在使用 Python 请求模块。我需要访问的页面是通过 POST 请求完成的。我公司有代理。我可以使用 requests.post() 中的代理标志通过代理。但是,有一个使用 cookie 的身份验证部分,我似乎无法通过它。使用 POST 请求时我应该如何进行身份验证部分?

我正在尝试使用此线程中描述的身份验证过程,但它不起作用: Authentication and python Requests

代码是这样设置的:

import ssl
from MyHtmlParser import MyHTMLParser
from lxml import html 
import requests 
from bs4 import BeautifulSoup as bs

def authenticate(s, url):
    headers = {'USER_NAME': 'me', 'PASSWORD': 'mypassword', '_Id': 'submit'}
    page=s.get(url)
    soup=bs(page.content)
    value=soup.form.find_all('input')[2]['value']
    headers.update({'value_name':value})
    auth = s.post(url, params=headers, cookies=page.cookies)

post_url_finance = 'https://opsdata<company>com/scripts/finance/finance.exe'
values_finance = {'EMPLOYEE_TOTAL': 'employeeId'}

proxies = {'http': 'http://proxy-<company>.com'}

page = requests.post(post_url_finance, data=values_finance, proxies=proxies) print page.content

但是,我又收到了这个错误:

$ python postUsingRequests.py
Traceback (most recent call last):
  File "postUsingRequests.py", line 53, in <module>
    page = requests.post(post_url_finance, data=values_finance, proxies=proxies) 
  File "C:\Python27\lib\site-packages\requests\api.py", line 109, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Python27\lib\site-packages\requests\api.py", line 50, in request
    response = session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 465, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests\sessions.py", line 573, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests\adapters.py", line 431, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

【问题讨论】:

    标签: python authentication post proxy python-requests


    【解决方案1】:

    您遇到的问题似乎是由不受信任的 SSL 证书引起的。

    最快的解决方法是设置verify=False。请注意,这将导致证书无法验证,并使您的应用程序面临安全风险。但正如您所提到的,它在安全网络中运行,所以这不是一个严重的问题。

    【讨论】:

    • 我用 verify=False 试过了,我得到了这个结果: $ python postUsingRequests.py C:\Python27 \lib\site-packages\urllib3\connectionpool.py:768:InsecureRequestWarning:正在发出未经验证的 HTTPS 请求。强烈建议添加证书验证。请参阅:urllib3.readthedocs.org/en/latest/security.htmlInsecureRequestWarning)
    • 我期待一个带有表格的页面被抓取。这是否意味着它不会将我带到该页面?
    • 好像如果我不通过身份验证,它不会向我显示表格。
    【解决方案2】:
    s = requests.session()
    s.auth = {'USER_NAME': '----', 'PASSWORD': '----'}
    pageCert = requests.post(post_url_finance, proxies=proxies, verify=False) 
    

    我将 s.auth 与 verify=False 一起使用。这给了我一个回复,而不是给我 SSL 错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-23
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      • 2019-09-03
      • 1970-01-01
      相关资源
      最近更新 更多