• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

python 搞代码 4年前 (2022-01-09) 13次浏览 已收录 0个评论

股票和基金一直是热门的话题,很多周围的人都选择不同种类的理财方式。就股票而言,肯定是短时间内收益最大化,这里我们需要用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 <div>本文来源gaodai.ma#com搞##代!^码@网3</div>executor.map(getalldata, shanghaicode):
    pass

到此这篇关于Python爬虫回测股票的实例讲解的文章就介绍到这了,更多相关Python爬虫回测股票的方法内容请搜索搞代码以前的文章或继续浏览下面的相关文章希望大家以后多多支持搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Python爬虫回测股票的实例讲解
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址