使用python获取请求的URL的上下文类型

时间:2014-08-11 18:37:00

标签: python

我正在尝试使用本教程的http://docs.python-requests.org/en/latest/使用python获取url的标题。我正在尝试在python空闲中跟随代码,我收到以下错误,

>>> import requests
>>> r = requests.get('https://api.github.com/user')

Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    r = requests.get('https://api.github.com/user')
  File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\api.py", line 55, in get
    return request('get', url, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\sessions.py", line 456, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\sessions.py", line 559, in send
    r = adapter.send(request, **kwargs)
  File "C:\Python27\lib\site-packages\requests-2.3.0-py2.7.egg\requests\adapters.py", line 375, in send
    raise ConnectionError(e, request=request)
ConnectionError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /user (Caused by <class 'socket.error'>: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions)

1 个答案:

答案 0 :(得分:0)

看起来github拒绝您访问所请求的页面。在尝试在python中请求页面之前,尝试在浏览器中键入它们以查看返回的内容。当我这样做时,我返回了一些JSON声明

{
    "message": "Requires authentication",
    "documentation_url": "https://developer.github.com/v3"
}

如果您想测试代码并查找网页标题,请在深入研究API之前尝试使用可公开访问的网页。