爬取股票数据

时隔三年,再次开篇。终究没逃出技术的圈子,继续努力吧!


话说最近股市又熊起了,哎错过了一次上车机会。

鉴于精力有限,股票太多支,不可能支支俱到,准备写个软件,筛选符合个人技术规则的股票。

第一步,获取股票的历史数据。

在此,感谢前面已经铺好路的博主们,python脚本直接使用。


import urllib.request
import re
import os
import datetime


#网址、目录、时间、内容
stock_CodeUrl = 'http://quote.eastmoney.com/stocklist.html'

db_path = 'D:\\code\\python\\db\\'

#time_range = "&start=20180101&end=20190211"
time_range = "&end=" + datetime.datetime.now().strftime('%Y%m%d')

data_format = time_range + '&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP'


#获取股票代码列表
def urlTolist(url):
    allCodeList = []
    html = urllib.request.urlopen(url).read()
    html = html.decode('gbk')
    s = r'
  • ' pat = re.compile(s) code = pat.findall(html) #print(code) for item in code: #if item[0]=='6' or item[0]=='3' or item[0]=='0': if item[0]=='6' or item[0]=='0': allCodeList.append(item) return allCodeList allCodelist = urlTolist(stock_CodeUrl) if not os.path.exists(db_path): os.makedirs(db_path) for code in allCodelist: print('正在获取%s股票数据...'%code) if code[0]=='6': url = 'http://quotes.money.163.com/service/chddata.html?code=0' + code + data_format else: url = 'http://quotes.money.163.com/service/chddata.html?code=1' + code + data_format file_name = db_path+code+'.csv' if not os.path.exists(file_name): urllib.request.urlretrieve(url,file_name)
  •  

     

    你可能感兴趣的:(股票数据)