东财数据爬取

# coding: utf-8
"""
# @Time    : 2021/5/24 8:57
# @Author  : Gina Gao
# @File    : 
# @Software: PyCharm
# @Descript:
"""
 
import pandas as pd

import openpyxl
import  requests
import  json


def write_excel_xlsx(path, sheet_name, value):
    print("print",1)
    # index = len(value)
    # print(value.keys())#['f1', 'f2']
    # print(value)
    dict_list=[]
    for i in value:
        for j in i:
            dict_list.append(j)
    index=len(dict_list)
    print(dict_list[0].keys())
    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = sheet_name
    for i in range(0, index):
        # for j in range(0, len(value[i])):
        # print(dict_list[i])
        sheet.cell(row=i+1, column=1, value=str(dict_list[i]))
    workbook.save(path)
    print("xlsx格式表格写入数据成功!")

def data_url():
    path='http://push2.eastmoney.com/api/qt/clist/get?fid=f62&po=1&pz=50&pn=1&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A13%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A7%2Bf%3A!2%2Cm%3A1%2Bt%3A3%2Bf%3A!2&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1'
    data=requests.get(path).content
    data_js=json.loads(data)
    total=data_js['data']['total']
    pn=int(total/50)
    print("total",total)
    if total%50!=0:
        pn=pn+1
    data_dict = {}
    data_detail=[]
    for i in range(1,pn):#pn 页数
        # print("i",i)
        path1='http://push2.eastmoney.com/api/qt/clist/get?fid=f62&po=1&pz=50&pn={i}&np=1&fltt=2&invt=2&ut=b2884a393a59ad64002292a3e90d46a5&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A13%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A7%2Bf%3A!2%2Cm%3A1%2Bt%3A3%2Bf%3A!2&fields=f12%2Cf14%2Cf2%2Cf3%2Cf62%2Cf184%2Cf66%2Cf69%2Cf72%2Cf75%2Cf78%2Cf81%2Cf84%2Cf87%2Cf204%2Cf205%2Cf124%2Cf1'.format(i=i)
        data = requests.get(path1).content
        data_js = json.loads(data)
        # print(data_js)
        data_detail.append(data_js['data']['diff'])


    return data_detail

def main():
    data=data_url()
    print("data",len(data))
    # print(data)
    filepath=r'C:\Users\kexin.gao\Desktop\2222222222222\test.xlsx'
    sheet_name='gina_test'
    write_excel_xlsx(filepath,sheet_name,data)
if __name__ == '__main__':
    main()

 

posted @ 2021-06-08 17:32  yongqi-911  阅读(225)  评论(0编辑  收藏  举报