I'm having a problem while tring to read data from a website using urllib in Python 3 that causes my program to stand still (it does not give me any error message, it looks like it is stucked on a loop). The weird part is that this same script works with any other websites that I try, except this one. I've been doing some research and read people talking about that "HTTP Error 403: Forbidden" traceback that can be fixed using a custom User-Agent to open the url as a browser, but this does not fix my problem.
I'm almost 100% sure that the problem is some type of restriction from the PC because I'm using a PC from work and both the PC and the Network has some restrictions here, but the weird thing is that the website opens when I'm using Chrome.
This code was posted by Kardi Teknomo on another topic and is an interesting way to show my problem.
import urllib.request
url = "http://py4e-data.dr-chuck.net/known_by_Fikret.html"
page = urllib.request.Request(url,headers={'User-Agent': 'Chrome/76.0.3809.132'})
infile = urllib.request.urlopen(page).read()
data = infile.decode()
print(data)
A simpler code that tries to do the same and fails in the same way, is this one:
import urllib.request, urllib.parse, urllib.error
Fhandle = urllib.request.urlopen("http://py4e-data.dr-chuck.net/known_by_Fikret.html")
for lines in Fhandle:
lines = lines.decode().strip()
print(lines)
I'm trying to do something more complex, but for the sake of fixing the problem, all I'm trying to do with this code is to read an HTML page and print its content, but the program always get stucked right after I press enter if this I use the URL from theses examples.