Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Get HTML Source Code of a Web Page

Tags:

python

urllib2

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?

like image 917
Sdrf1445 Avatar asked Sep 06 '25 03:09

Sdrf1445


1 Answers

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
like image 84
Skiller Dz Avatar answered Sep 07 '25 21:09

Skiller Dz