Please help, my code throws this error:
Traceback error (most recent call last):
…
Here is my code:
import requests
data = requests.get('https://api.github.com/user')
webpage_text = data.text
print(webpage_text)
Please help, my code throws this error:
Traceback error (most recent call last):
…
Here is my code:
import requests
data = requests.get('https://api.github.com/user')
webpage_text = data.text
print(webpage_text)
You did not include the details from the traceback.
So no one can know for sure what happened.
But my wild guess is you got a 401 error like I do when I try that URL:
% curl https://api.github.com/user
{
"message": "Requires authentication",
"documentation_url": "https://docs.github.com/rest/users/users#get-the-authenticated-user",
"status": "401"
}
As you can see the response contains the reason it failed.
Error details:
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 159, in _new_conn
conn = connection.create_connection(
File "/usr/lib/python3.8/site-packages/urllib3/util/connection.py", line 61, in create_connection
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Try again
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 670, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 381, in _make_request
self._validate_conn(conn)
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 976, in _validate_conn
conn.connect()
File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 308, in connect
conn = self._new_conn()
File "/usr/lib/python3.8/site-packages/urllib3/connection.py", line 171, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7f3c8a5c6400>: Failed to establish a new connection: [Errno -3] Try again
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/usr/lib/python3.8/site-packages/urllib3/connectionpool.py", line 724, in urlopen
retries = retries.increment(
File "/usr/lib/python3.8/site-packages/urllib3/util/retry.py", line 439, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /users/python (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f3c8a5c6400>: Failed to establish a new connection: [Errno -3] Try again'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 2, in <module>
data = requests.get("https://api.github.com/users/python")
File "/usr/lib/python3.8/site-packages/requests/api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3.8/site-packages/requests/sessions.py", line 643, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3.8/site-packages/requests/adapters.py", line 516, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.github.com', port=443): Max retries exceeded with url: /users/python (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f3c8a5c6400>: Failed to establish a new connection: [Errno -3] Try again'))
** Process exited - Return Code: 1 **
Press Enter to exit terminal
Seems that you have a DNS issue or a routing issue not a python issue.
Check dns with host api.github.com
And check connection with curl https://api.github.com/user
I get this same error, no matter where I try it from, and even if I just put in google.com as the site. I’ve tried from an online Python compiler, from a Snowflake function. Curl works fine for me. I appreciate any help!
I have already found a solution:
import requests
try:
ans = requests.get('https://google.com/').content
except:
ans = 'No internet connectivity.'
print(ans)
(Just in case, I tested the code on the google.colab, it work,
but on the Online Python it doesn’t work)
@aa-shawntracy
Very interesting – it appears to be an issue with that online-python site (as well as my snowflake instance). It works fine in the google.colab. I’m very new to python, any idea what would be different between the two, that would allow google.colab to connect, and the others not to be able to?