How to get data for a stock list

by: chgchksg128, 9 years ago


Hi,
I am reading your code as below link
https://pythonprogramming.net/create-a-stock-screener-python/?completed=/price-to-book-ratio/
I notice that all the stocks has to be key in one by one.
sp500 = ['a', 'aa', 'aapl', 'abbv', 'abc', 'abt', 'ace', 'aci', 'acn', 'act', 'adbe', 'adi', 'adm', 'adp', 'adsk', 'adt', 'aee', 'aeo', 'aep', 'aes', 'aet', 'afl', 'agn', 'aig', 'aiv', 'aiz', 'akam', 'all', 'altr', 'alxn', 'amat', 'amd', 'amgn', 'amp', 'amt', 'amzn', 'an', 'anf', 'ann', 'aon', 'apa', 'apc', 'apd', 'aph', 'apol', 'arg', 'arna', 'aro', 'ati', 'atvi', 'avb', 'avp', 'avy', 'axp', 'azo', 'ba', 'bac', 'bax', 'bbby']

Is there a way to grab all the data from Yahoo fiance without manually typing the list? There are some newly listing stocks and we have to update manually every time.
Also, since I am trying to get data from KLSE, Malaysia stocks exchange which using code like 0165.KL,3182.KL etc. Is it a way how can I create a list without manually typing for KLSE?
Thanks




You must be logged in to post. Please login or register an account.



You could find a source online that lists out stocks, and parse it with Python.

-Harrison 9 years ago

You must be logged in to post. Please login or register an account.


from lxml import html
import requests
import csv
page = requests.get('http://www.google.com/finance?q=[%28exchange+%3D%3D+%22KLSE%22%29]&restype=company&noIL=1&start=0&num=1500')
tree = html.fromstring(page.content)

#Scrape stocks companies and symbols
    
stocks = tree.xpath('//a [not(@class)][@id][@href]/text()')
#This will create a list of prices

print 'Stocks= ', stocks
  
# open a file for writing.
csv_out = open('KLSE.csv', 'wb')

# create the csv writer object.
mywriter = csv.writer(csv_out)

# all rows at once.
rows = zip(stocks)
mywriter.writerows(rows)
# always make sure that you close the file.
# otherwise you might find that it is empty.
csv_out.close()

I try to create the list but it is arrange in rows.
How can I able to load the output in csv into the S&P array format?
Please advice


      


-chgchksg128 9 years ago

You must be logged in to post. Please login or register an account.