Demo entry 3320249

python code

   

Submitted by anonymous on Dec 14, 2015 at 17:38
Language: Python 3. Code size: 891 Bytes.

import re
from robobrowser import RoboBrowser
import pandas
import urllib

wdPath = "/Users/hichamelboukkouri/Documents/Studies/ENSAE/2A/S1/Python pour DS/ProjetPython/stockData/"

def Scrape(wdPath):

    browser = RoboBrowser(history=True)
    browser.open('https://en.wikipedia.org/wiki/List_of_S%26P_500_companies',)
    
    htmlList = [ re.findall('[A-Z]*</a>$',str(html)) for html in browser.find_all('a')]
    symbolList = [ element[0].replace(' ', '')[:-4].upper() for element in htmlList if element[0].replace(' ', '')[:-4].upper() != '']
    
    for symbol in symbolList :
        try :
            pandas.DataFrame.from_csv("http://real-chart.finance.yahoo.com/table.csv?s="+ symbol +"&amp;d=9&amp;e=12&amp;f=2015&amp;g=d&amp;a=11&amp;b=12&amp;c=1980&amp;ignore=.csv").to_csv(wdPath + symbol + '_data')
        except urllib.error.HTTPError :
            pass

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).