4

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?

4
  • Get started here stackoverflow.com/questions/2792650/… Commented Aug 23, 2018 at 18:39
  • Is there any particular reason to use urllib2 here? Because i would suggest: from requests import request with a usage like resp = request('GET', <url>).pageSource = resp.text. Also what version of python are you using? Commented Aug 23, 2018 at 18:44
  • Btw: this might be relevant to your problem from somebody who wanted to use urllib2 in python3: thread Commented Aug 23, 2018 at 18:50
  • 1
    Possible duplicate of Import error: No module name urllib2 Commented Aug 23, 2018 at 20:38

1 Answer 1

14

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
Sign up to request clarification or add additional context in comments.

1 Comment

Requests works but can't install urllib2 , recieving errors

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.