Python爬取股票数据
配置环境:python 3.7 python编辑器:pycharm
整理成代码如下:
import urllib.request import os,shutil import glob import time import datetime list = [ '000883', '002564', '000413', '600477', '300259', '002027', '002717', '600050', '000100', '000725', '002152', '603711', '603848' ] # current = time.strftime('%Y-%m-%d',time.localtime(time.time())) today = datetime.date.today().strftime("%Y%m%d") for a in list: print('正在获取%s股票数据...' % a) if (a[0] == '6' or a[0] == '9'): url = 'http://quotes.money.163.com/service/chddata.html?code=0' + a + \ '&start=20200312&end=now&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP' print(url) else: url ='http://quotes.money.163.com/service/chddata.html?code=1' + a + \ '&start=20200312&end=now&fields=TCLOSE;HIGH;LOW;TOPEN;LCLOSE;CHG;PCHG;TURNOVER;VOTURNOVER;VATURNOVER;TCAP;MCAP' print(url) urllib.request.urlretrieve(url,'F:\\python股票爬取数据\\' + a + '.csv') csvx_list = glob.glob('F:\\python股票爬取数据\\*.csv') print('总共发现%s个CSV文件' % len(csvx_list)) time.sleep(2) print('正在处理............') for i in csvx_list: fr = open(i, 'r').read() with open(r'F:\python股票爬取数据\merge.csv', 'a') as f: # 合并csv文件 f.write(fr) print('写入完毕!') folder ="F:\\python股票爬取数据\\merge.csv" file ="F:\\python股票爬取数据\\" + datetime.date.today().strftime("%Y%m%d") while True == os.path.exists(file): file = file +datetime.date.today().strftime("%Y%m%d") os.makedirs(file) shutil.move(folder,file) print("移动完毕")