JSON and Scraping
#
# Print all S&P stocks listed on Wikipedia
#
import requests
from bs4 import BeautifulSoup
# Query for the HTML
data = requests.get('http://en.wikipedia.org/wiki/List_of_S%26P_500_companies#S&P_500_component_stocks')
soup = BeautifulSoup(data.text,'html.parser')
# Get the one <table> from the webpage.
maintable = soup.find('table')
# Get all <a> links inside this <table>
links = maintable.find_all('a')
for link in links:
# Skip "reports" links.
if link.text != 'reports':
aclass = link.get('class')
if aclass != None and 'external' in aclass:
print(link.text)