欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

Python爬虫回测股票的实例讲解

时间:2021-07-22 07:47:31|栏目:Python代码|点击:

股票和基金一直是热门的话题,很多周围的人都选择不同种类的理财方式。就股票而言,肯定是短时间内收益最大化,这里我们需要用python爬虫的方法,来帮助我们获取一些股票的数据,这样才能更好的买到相应的股票。下面我们就python爬虫获取股票数据的方法带来详细的讲解。

1.生成上证与深证所有股票的代码:

#上证代码
shanghaicode = []
for i in range(600000, 604000, 1):
  shanghaicode.append(str(i))
#深证代码
shenzhencode = []
for i in range(1000000, 1005000, 1):
  i = str(i)[1:] 
  shenzhencode.append(i)

2.定义一个爬取函数,broker创建的实例:

def getalldata(code):
    if os.path.exists(datapath + code + '.csv'):
      print(code + 'already existed!')
      return
    metadata = broker.get_stock_pro(code)
    if len(metadata) == 0:
      return
    metadata.to_csv('C:/Users/abc/Desktop/' + code + '.csv',index = False)
    print(code + 'finished!')

3.导入多线程需要的模块

from concurrent.futures.thread import ThreadPoolExecutor #多线程

4.遍历所有代码开始爬取,max_workers可适当调整

  executor = ThreadPoolExecutor(max_workers=3)
  for datatemp in executor.map(getalldata, shenzhencode):
    pass 
  executor = ThreadPoolExecutor(max_workers=3)
  for datatemp in executor.map(getalldata, shanghaicode):
    pass

上一篇:Python获取昨天、今天、明天开始、结束时间戳的方法

栏    目:Python代码

下一篇:Python实现自动整理文件的脚本

本文标题:Python爬虫回测股票的实例讲解

本文地址:http://www.codeinn.net/misctech/160275.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有