Python下载文件的代码

通过Python下载中证行业分类文件

import requests

url = 'http://www.csindex.com.cn/uploads/downloads/other/files/zh_CN/ZzhyflWz.zip'
r = requests.get(url, stream = True)
with open("D:\\zhongzheng.zip", "wb") as Pypdf:
    for chunk in r.iter_content(chunk_size = 1024):
        if chunk:
            Pypdf.write(chunk)

通过Python下载网易金融数据文件

import requests

url = 'http://quotes.money.163.com/service/zycwzb_300525.html?type=report'
myfile = requests.get(url, allow_redirects=True)
open('D:\\300525.csv', 'wb').write(myfile.content)

解压缩zip文件

import zipfile

zip_file = zipfile.ZipFile("D:\\zhongzheng.zip")
print zip_file.namelist()
zip_file.extractall(path="D:\\stockdata")