I can run the below code fine on my MacBook at home, but when running the same code at work I get a traceback error. I think the issue may be a network issue but I'm not 100% sure so wanted to share the code and traceback here to see if a more experienced individual may be able to confirm or have a better idea of what is going wrong! (the same sort of error come up when I try to use the googlesearch-python library as well).
I am trying to run the following code:
import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup
import ssl
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
url = 'http://py4e-data.dr-chuck.net/known_by_Ann.com'
x=0
while x <7:
x +=1
html = urllib.request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html, 'html.parser')
tags = soup('a')
url = tags[17].get('href', None)
print('Retrieving:',url)
print(tags[17].contents[0])
And I get the following error:
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1354, in do_open
h.request(req.get_method(), req.selector, req.data, headers,
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1255, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1301, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1250, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 1010, in _send_output
self.send(msg)
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 950, in send
self.connect()
File "C:\ProgramData\Anaconda3\lib\http\client.py", line 921, in connect
self.sock = self._create_connection(
File "C:\ProgramData\Anaconda3\lib\socket.py", line 787, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "C:\ProgramData\Anaconda3\lib\socket.py", line 918, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/u49063/Spittalj/env/Test Code/urllink_ex1.py", line 15, in <module>
html = urllib.request.urlopen(url, context=ctx).read()
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 525, in open
response = self._open(req, data)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 542, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1383, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "C:\ProgramData\Anaconda3\lib\urllib\request.py", line 1357, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11001] getaddrinfo failed>
I am running the code as a test to see if I can pull hyperlinks from a webpage and then go into those said hyperlinks.
The code failed with the above mentioned traceback - I think it may be a network / internet connection issues. (I am trying to run the code at work - when done at home on my personal laptop it works fine).