请求:无法转换' NoneType'隐含地反对str

时间:2017-08-27 20:12:53

标签: python get python-requests

我正在尝试使用此网址执行GET请求:

http://g.api.mega.co.nz

当请求给我以下错误时:

>>> r = requests.get("http://g.api.mega.co.nz")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/requests/api.py", line 67, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 53, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 468, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 597, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 597, in <listcomp>
    history = [resp for resp in gen] if allow_redirects else []
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 195, in resolve_redirects
    **adapter_kwargs
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 576, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 343, in send
    conn = self.get_connection(request.url, proxies)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 250, in get_connection
    proxy = select_proxy(url, proxies)
  File "/usr/lib/python3/dist-packages/requests/utils.py", line 575, in select_proxy
    proxy = proxies.get(urlparts.scheme+'://'+urlparts.hostname)
TypeError: Can't convert 'NoneType' object to str implicitly
>>> 

我在python2.7python3.5中对其进行了测试,两者都在语义上给出了相同的错误。 我认为长子域链给出了错误,但我通过使用通配符dns配置在我的服务器上使用长子域链进行测试而反驳了这一点。

我可以看到host g.api.mega.co.nz该网址是lu.api.mega.co.nz. 的别名

尝试使用curl --head和http访问该网址时,会给我以下内容:

HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Content-Type, MEGA-Chrome-Antileak
Access-Control-Expose-Headers: Original-Content-Length
Access-Control-Max-Age: 86400

当我尝试使用https访问因为301状态代码时,我得到一个空响应。

情节变浓。

1 个答案:

答案 0 :(得分:1)

根据requests/urllib库,当服务无法重定向到严格说出无效位置时,会发生此错误。

Simliar: LocationValueError when using python requests module

每次我遇到这个服务时都会提供https,这可能是这个问题的根源。

在上面的示例中,服务重定向到Location: https:// ,其中requests会将主机名解析为导致错误的None

在另一个示例sensetime.com中,网址重定向到:

...
< Connection: keep-alive
< Location: https:www.sensetime.com/
<
...

哪个urllib无法解析(因为缺少反斜杠而失败。

你应该做什么/我做了什么:

  • 做一个全能的,把这些网址放在你遇到的地方的某个单独的地方,稍后你可以通过https来连接它们

  • 更新你的请求库(pip install --upgrade requests):我最新的版本给了我一个不同的错误信息,这让我找到了这个问题的答案