Skip to content

Instantly share code, notes, and snippets.

@akshaykarnawat
Created June 6, 2017 04:18
Show Gist options
  • Save akshaykarnawat/7b041a800b1d93c855253c49a2aa1c1d to your computer and use it in GitHub Desktop.
Save akshaykarnawat/7b041a800b1d93c855253c49a2aa1c1d to your computer and use it in GitHub Desktop.
List of S&P 500 companies from Wikipedia
import requests
from bs4 import BeautifulSoup
def getWikipediaPageWithListOfStocks():
r = requests.get('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies')
if(r.status_code != 200):
return
return processDocument(r.text)
def processDocument(text):
list = []
perfectSoup = BeautifulSoup(text, 'html.parser')
table = perfectSoup.find_all('table')[0]
for row in table.find_all('tr'):
for column in row.find_all('td')[0:1]:
if(column.a is None):
continue
list.append(column.a.string)
return list
def getListOfStocks():
return getWikipediaPageWithListOfStocks()
#test
print(getListOfStocks())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment