User Tools

Site Tools


clubs:python_club:python_club_ex_get_webpage
Home | clubs :: cloud club :: python_club :: 3D-Printing | projects :: Proxmox | Kubernetes | scripting | utilities | games

About the Club

Python Club Topics - Exercise: Get webpage status

Exercise: Get webpage

  1. Output: Display the number of each vowel in a given string.
  2. What you learn from the example:
    1. Manipulate strings
    2. Use the built-in count function
    3. use a dictionary

Solution

[1] code:python show
# Import the 'requests' module for making HTTP requests.
import requests

# Define the URL of the web page to be accessed.
url = 'http://www.example.com/'

# Define headers to mimic a user agent (browser) for the request.
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh) Gecko/20100101 Firefox/38.0'}

# Use the 'get' method from the 'requests' module to make an HTTP request to the specified URL with headers.
request = requests.get(url, headers=headers)

# Print the status of the web page request.
print('Web page status: ', request)

# Print HTML code of the web page if the request was successful (status code 200).
print('\nHTML code of the above web page:')
if request.ok:
    print(request.text)
clubs/python_club/python_club_ex_get_webpage.txt · Last modified: by 127.0.0.1