Skip to content

Instantly share code, notes, and snippets.

@IT102Gists
Created October 17, 2018 16:18
Show Gist options
  • Save IT102Gists/a336617a31e31aecc11f92a68e1ecca3 to your computer and use it in GitHub Desktop.
Save IT102Gists/a336617a31e31aecc11f92a68e1ecca3 to your computer and use it in GitHub Desktop.
Python CodeAlong: an intro to the Zen of Python and webscraping with Beautiful Soup.
# standard library
import random
from urllib.request import urlopen
# third-party
from bs4 import BeautifulSoup
# make an HTTP request to get the Zen of Python
url = "https://www.python.org/dev/peps/pep-0020/"
html = urlopen(url)
# create a Beautiful Soup object
soup = BeautifulSoup(html, "html.parser")
# find the first class match
zen_of_python = soup.find("pre", {"class": "literal-block"}).get_text()
# process the lines
lines = zen_of_python.split("\n")
# remove leading and trailing new lines
clean_lines = lines[1:-1]
# display a random line from Zen of Python
print(random.choice(clean_lines))
### FURTHER READING ###
# Find PEP 20 -- The Zen of Python -- at
# https://www.python.org/dev/peps/pep-0020/
# Read more about web scraping with Beautiful Soup at
# https://pypi.org/project/beautifulsoup4/
# Requests is another highly recommended third-party HTTP package for Python:
# http://docs.python-requests.org/en/master/
# Looking for a free place to store your programs online?
# Try GitHub Gists: https://gist.github.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment