Python - View Page Source

Python - Get Page Source from URL

  • Python
  • 2 mins read

Here I am giving an example of a Python program to get page source from the URL.

You will get the same web page source as you used to view by doing the right-click on the web page and then select the option View Page Source as shown in the image above. Below is an example:

Python Program to Get Web Page Source

import requests

v_url='http://www.python.org'

r = requests.get(v_url)

page_source = r.content

print(page_source)

Output (Displaying few lines from actual production)

<!doctype html>
<!--[if lt IE 7]>   <html class="no-js ie6 lt-ie7 lt-ie8 lt-ie9">   <![endif]-->
<!--[if IE 7]>      <html class="no-js ie7 lt-ie8 lt-ie9">          <![endif]-->
<!--[if IE 8]>      <html class="no-js ie8 lt-ie9">                 <![endif]-->
<!--[if gt IE 8]><!--><html class="no-js" lang="en" dir="ltr">  <!--<![endif]-->

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <link rel="prefetch" href="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">

See also: