How to Download a Webpage in Python

The following python program demonstrates the use of urllib module to download a webpage to a local folder. Please note that this program downloads the webpage html content only, it doesn't download the linked images or other resources. The following program also demonstrates use of exception handling.

# python 3 only
import urllib.request
from urllib.error import URLError

target_file_path = "/Users/user/downloaded.html" # downloaded page saved here

    response = urllib.request.urlopen('')
    html_content =

    with open(target_file_path,"wb") as fp:

except URLError as e:
    print("Unable to download page: "+str(e.reason))

If you are looking for a way to scrap entire web pages including resources, you should look at the scrapy library. If you are more interested in the parsing of the web content, you should look at Beautiful Soup.