代理URL必须具有显式方案

时间:2016-03-06 08:26:33

标签: python proxy httprequest python-requests

我想在请求中设置代理并获取url源代码的内容:

privoxy = 'localhost:8118'
proxy_dict = {'http':privoxy, 'https':privoxy, 'ftp':privoxy, 'ssl':privoxy}
content = session.get(url=url, proxies=proxy_dict).content

以下错误:

 File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 467, in get
    return self.request('GET', url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 455, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 558, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 305, in send
    conn = self.get_connection(request.url, proxies)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 206, in get_connection
    except_on_missing_scheme(proxy)
  File "/usr/lib/python2.7/dist-packages/requests/utils.py", line 636, in except_on_missing_scheme
    raise MissingSchema('Proxy URLs must have explicit schemes.')
requests.exceptions.MissingSchema: Proxy URLs must have explicit schemes.

1 个答案:

答案 0 :(得分:2)

在您的情况下将scheme设置为http

privoxy = 'http://localhost:8118'

(当然可以是其他人,比如'https','ftp'等)

来自Requests docs for Proxies的更多内容:

  

请注意,代理网址必须包含该方案。