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?

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Sdrf1445
  • 147
  • 2
  • 2
  • 9
  • Get started here https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2 – SoConfused Aug 23 '18 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', )`.`pageSource = resp.text`. Also what version of python are you using? – garglblarg Aug 23 '18 at 18:44
  • Btw: this might be relevant to your problem from somebody who wanted to use urllib2 in python3: [thread](https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2) – garglblarg Aug 23 '18 at 18:50
  • 1
    Possible duplicate of [Import error: No module name urllib2](https://stackoverflow.com/questions/2792650/import-error-no-module-name-urllib2) – glennsl Aug 23 '18 at 20:38

1 Answers1

13

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
Skiller Dz
  • 897
  • 10
  • 17