Search code examples
pythonurllib2

Python - Get HTML Source Code of a Web Page


I want to get the HTML source from a site ('example.com' for example).

I tried the following:

import urllib2

response = urllib2.urlopen("https://example.com")
page_source = response.read()

It says:

'No module named urllib2'

How can I prevent this error?


Solution

  • why you don't use requests module ? :

    import requests
    
    r = requests.get("https://example.com")
    print r.text
    

    or for answer correctly to you'r question , you can download the urllib2 module using pip and easy_install :

    pip install urllib2
    easy_isntall urllib2
    

    for requests:

    pip install requests
    easy_install requests
    

    for requests , you should install urllib3:

    pip install urllib3
    easy_install urllib3