0

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.

2
  • this exact code works fine on my machine. It might be a network issue Commented Sep 26, 2019 at 16:11
  • Yeah, that's what I was affraid of. Still, let me see if someone have a workaround solution that make it work. Commented Sep 26, 2019 at 16:27

1 Answer 1

0

I have had the same issue while running code with urllib.request.urlopen(link, context=ctx).read() in IDE PyCharm . The code runs perfectly in console. In order to run this code in IDE, once you put url via url = input('Enter url: ') move the cursor to the beginning of the line and then press 'Enter'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.