1

将 Crawlera 的示例代码用于带有代理的 GET 请求。

import requests

url = "http://httpbin.org/ip"
proxy_host = "proxy.crawlera.com"
proxy_port = "8010"
proxy_auth = "<APIKEY>:" # Make sure to include ':' at the end
proxies = {
      "https": "https://{}@{}:{}/".format(proxy_auth, proxy_host, proxy_port),
      "http": "http://{}@{}:{}/".format(proxy_auth, proxy_host, proxy_port)
}

r = requests.get(url, proxies=proxies, verify=False)

我收到 407 Bad Proxy Auth 错误。我已经检查了 API_KEY 是否正确的三倍。

响应标头:

{
   'Proxy-Connection': 'close',
   'Proxy-Authenticate': 'Basic realm="Crawlera"',
   'Transfer-Encoding': 'chunked',
   'Connection': 'close',
   'Date': 'Mon, 26 Mar 2018 11:18:05 GMT',
   'X-Crawlera-Error': 'bad_proxy_auth',
   'X-Crawlera-Version': '1.32.0-07c786'
}

请求已更新。

$ pip freeze |grep requests
requests==2.8.1
4

2 回答 2

0

如果您想保留“爬虫”方式,您可以尝试升级您的请求客户端:

pip install requests --upgrade

我遇到了同样的问题,您的解决方案有效,但经过进一步搜索,我找到了这个解决方案:

requests将客户端升级到2.19 ...对我有用,我可以继续使用 Crawlera 示例脚本。

于 2018-06-19T17:36:07.500 回答
0

我设法通过添加Proxy-Authorization标题使其工作。

proxy_auth  = "<APIKEY>:"
headers = {
   # other headers ...
   "Proxy-Authorization": 'Basic ' + base64.b64encode(proxy_auth)
}

proxies = {
      "https": "https://{}:{}/".format(proxy_host, proxy_port),
      "http": "http://{}:{}/".format(proxy_host, proxy_port)
}


r = requests.get(url, headers=headers, proxies=proxies, verify=False)
于 2018-03-26T22:47:27.287 回答