python response重头开始_如何使用Python获取所有的股票代码?

话不多说先上代码:

import os#这里可能需要pip install requestsimport requestsfrom requests.adapters import HTTPAdapter#打开日志文件fs = open(r'C:\Py\codes.txt','a+')#股票代码strCode = ''#获取上证股票for index in range(0,6000):  if len(strCode) == 0:    strCode = 'sh' + str(600000 + index)  else:    strCode = strCode + ',sh' + str(600000 + index)  if index % 50 == 0 or index == 5999:    try:      s = requests.Session()      s.mount('http://', HTTPAdapter(max_retries=3))      response = s.get('http://hq.sinajs.cn/list=' + strCode, timeout=5)      text = response.text      strs = text.split(';')      for subStr in strs:        if len(subStr) > 100:          stockCode = subStr[subStr.find('var hq_str_') + len('var hq_str_') : subStr.find('=')]          stockName = subStr[subStr.find("\"") + 1 : subStr.find(',')]          print(stockCode + ',' + stockName)          fs.write(stockCode + ',' + stockName + '\r\n')    except requests.exceptions.RequestException as e:          print(e)    strCode = ''#深证股票for index in range(0,4000):  if len(strCode) == 0:    strCode = 'sz' + str(1000000 + index)[1 : 7]  else:    strCode = strCode + ',sz' + str(1000000 + index)[1 : 7]  if index % 50 == 0 or index == 3999:    try:      s = requests.Session()      s.mount('http://', HTTPAdapter(max_retries=3))      response = s.get('http://hq.sinajs.cn/list=' + strCode, timeout=5)      text = response.text      strs = text.split(';')      for subStr in strs:        if len(subStr) > 100:          stockCode = subStr[subStr.find('var hq_str_') + len('var hq_str_') : subStr.find('=')]          stockName = subStr[subStr.find("\"") + 1 : subStr.find(',')]          print(stockCode + ',' + stockName)          fs.write(stockCode + ',' + stockName + '\r\n')    except requests.exceptions.RequestException as e:          print(e)    strCode = ''#创业板股票for index in range(0,1200):  if len(strCode) == 0:    strCode = 'sz' + str(300000 + index)  else:    strCode = strCode + ',sz' + str(300000 + index)  if index % 50 == 0 or index == 1199:    try:      s = requests.Session()      s.mount('http://', HTTPAdapter(max_retries=3))      response = s.get('http://hq.sinajs.cn/list=' + strCode, timeout=5)      text = response.text      strs = text.split(';')      for subStr in strs:        if len(subStr) > 100:          stockCode = subStr[subStr.find('var hq_str_') + len('var hq_str_') : subStr.find('=')]          stockName = subStr[subStr.find("\"") + 1 : subStr.find(',')]          print(stockCode + ',' + stockName)          fs.write(stockCode + ',' + stockName + '\r\n')    except requests.exceptions.RequestException as e:          print(e)    strCode = ''#科创版股票for index in range(0,1000):  if len(strCode) == 0:    strCode = 'sh' + str(688000 + index)  else:    strCode = strCode + ',sh' + str(688000 + index)  if index % 50 == 0 or index == 999:    try:      s = requests.Session()      s.mount('http://', HTTPAdapter(max_retries=3))      response = s.get('http://hq.sinajs.cn/list=' + strCode, timeout=5)      text = response.text      strs = text.split(';')      for subStr in strs:        if len(subStr) > 100:          stockCode = subStr[subStr.find('var hq_str_') + len('var hq_str_') : subStr.find('=')]          stockName = subStr[subStr.find("\"") + 1 : subStr.find(',')]          print(stockCode + ',' + stockName)          fs.write(stockCode + ',' + stockName + '\r\n')    except requests.exceptions.RequestException as e:          print(e)    strCode = ''#关闭文件流fs.close()

新建一个文件,命名为ALLCODES.py,并将上述代码粘贴到你的文件中。

3dda121d38187e0b17e8eccda97d4bd6.png

02864e3afaf403c44daaf9ad0e3e1977.png

如果没有安装Python,就到这个地址下载安装一下:

https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe

注意第一个界面的Add to Path一定要勾上。

然后你需要安装Pip,下载地址是:

https://bootstrap.pypa.io/get-pip.py

保存到电脑中,在命令行中输入如下命中后执行:

6cbfbc11a3652dd8f531dcdc3d163c92.png

cd582f6e260e1cf01ad8eea0180ae6c1.png

安装成功后,在用pip来安装http服务requests:

9be126fbc1e74ca88ec97f19cdfac85d.png

996d3f9f60a1aef37384404dcd4fd6d8.png

打开命令提示行,输入python C:\PY\ALLCODES.py

c1e3d212367cd91e87d424991d176884.png

输入回车运行脚本,得到如下结果:

0b8458a74fbab6ef0395e1ace828d2ec.png

result.txt中也输出了结果:

260cda83eb9a8dcab65bf7e8454a61ca.png

网上能自动拉取实时码表的接口极少,这里采用了试错法,例如上证指数是从600000开始的,然后不断累加1,用新浪接口来查询是否存在行情,如果存在就摘取名称和其他信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值