alexanderholt
10/25/2017 - 3:10 PM

Prettify Get Version

import requests

html = requests.get('http://econpy.pythonanywhere.com/ex/001.html')

html.text

# convert HTML into a structured Soup object
from bs4 import BeautifulSoup
b = BeautifulSoup(html.text)

print(b.prettify())

# 'find' method returns the first matching Tag (and everything inside of it)
print(b.find(name='body').prettify())

# .text will return the text without the extra tags
print(b.find(name='body').text)

# find_all will return all matching tags
print(b.find_all('div'))

b.find_all('div', title='buyer-name')
# beautiful soup will let us choose specific elements within div tags.

#We can use for loops to select just the text
for i in b.find_all('div', title='buyer-name'):
    print(i.text)
    
# Now I have a list of all names on the page
# What if I wanted a list of the prices?
for i in b.find_all('span'):
    print(i.text)