爬取股票数据

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


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

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

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

在此,感谢前面已经铺好路的博主们,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'<li><a target="_blank" href="http://quote.eastmoney.com/\S\S(.*?).html">'
    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)

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值