get web page source code in python

You can get a web page's source code in Python using the requests or urllib library. Here are examples using both libraries:

main.py
import requests 

url = "https://www.example.com"
response = requests.get(url)
source_code = response.text
print(source_code)
126 chars
7 lines
main.py
import urllib.request 

url = "https://www.example.com"
response = urllib.request.urlopen(url)
source_code = response.read()
print(source_code)
144 chars
7 lines

Both examples send an HTTP GET request to the specified URL and retrieve the web page's source code as a string. The second example uses urllib's urlopen function to retrieve the web page's content. Note that you can also use additional libraries like BeautifulSoup to parse the HTML source code.

gistlibby LogSnag