0

Im looking for a really simple code to call a url and print the html source code. This is what I am using. Im following an online course which has the code

def get_page(url):
try:
    import urllib
    return urllib.open(url).read()
except:
    return ""

print(get_page('https://www.yahoo.com/'))

Prints nothing but also no errors. Alternatively from browsing these forums I've tried

from urllib.request import urlopen

print (urlopen('https://xkcd.com/353/'))

when I do this I get

<http.client.HTTPResponse object at 0x000001E947559710>
1
  • Check this out print source Commented Mar 11, 2017 at 8:37

3 Answers 3

0
from urllib.request import urlopen    
print (urlopen('https://xkcd.com/353/').read().decode())
Sign up to request clarification or add additional context in comments.

Comments

0

Assuming UTF-8 encoding was used

from urllib import request
def get_src_code(url):
    r = request.urlopen("url")
    byte_code = r.read()
    src_code = bytecode.decode()
    return src_code

Comments

0

It prints the empty string at the except block. Your code is generating error because there is no attribute called open in urllib module. You can't see the error because you are using a try-except block which is returning an empty string on every error. In your code, you can see the error like this:

def get_page(url):
    try:
        import urllib
        return urllib.open(url).read()
    except Exception as e:
        return e.args[0]

To get your expected output, do it like this:

def get_page(url):
    try:
        from urllib.request import urlopen
        return urlopen(url).read().decode('utf-8')
    except Exception as e:
        return e.args[0]

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.