Skip to content

Instantly share code, notes, and snippets.

@BensonMuriithi
Last active August 7, 2016 07:51
Show Gist options
  • Save BensonMuriithi/5d3695270045bcc92902e57cfc6a7702 to your computer and use it in GitHub Desktop.
Save BensonMuriithi/5d3695270045bcc92902e57cfc6a7702 to your computer and use it in GitHub Desktop.
My solution for 'Decode A Web Page Two' exercise on practicepython.org -> http://www.practicepython.org/exercise/2014/07/14/19-decode-a-web-page-two.html
"""Fetch an online article and print it to the screen"""
import requests
from bs4 import BeautifulSoup
def getportions(soup):
heading = soup.find("div", {"class":"dek"})
if heading:
yield heading.text
for p in soup.find_all("p", {"class":""}):
yield p.text
def readpage(address):
page = requests.get(address)
soup = BeautifulSoup(page.text, "html.parser")
for s in getportions(soup):
print "\n%s" % s.encode("utf-8")
raw_input("\nPress enter to continue ")
print "End of article"
if __name__ == "__main__":
readpage("http://www.vanityfair.com/style/society/2014/06/monica-lewinsky-humiliation-culture")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment