Created
September 15, 2014 23:37
-
-
Save browny/fa6801357f59d8518583 to your computer and use it in GitHub Desktop.
Get TW stock name by its number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# requirements: | |
# beautifulsoup4==4.3.2 | |
# uniout==0.3.7 | |
import sys | |
import urllib2 | |
import re | |
import uniout | |
from bs4 import BeautifulSoup | |
from pprint import pprint | |
arg_list = sys.argv | |
stock_dict = {} | |
def is_number(s): | |
try: | |
float(s) | |
return True | |
except ValueError: | |
return False | |
def setup(): | |
url = "http://isin.twse.com.tw/isin/C_public.jsp?strMode=2" | |
page = urllib2.urlopen(url).read().decode('big5-hkscs') | |
soup = BeautifulSoup(page) | |
table = soup.find('table', attrs={'class':'h4'}) | |
rows = table.find_all('tr') | |
for row in rows: | |
cols = row.find_all('td') | |
l = cols[0].get_text().split() | |
if (is_number(l[0])): | |
stock_dict[l[0]] = l[1] | |
def get_name(stock_number): | |
return stock_dict[stock_number] | |
def main(): | |
setup() | |
print get_name(arg_list[1]) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment