python爬取网页图片显示的原始时间序列数据,如基金时间序列净值,收益,仓位。 并整理成规范的日期和多序列格式

环境:
python 2.7 安装 pandas numpy等基础包 此处直接下载aconda2比较好.
基于win10的写的,但是win7也无差的。

包 软件等 我下的找个,大家可以用 和我用一样的 省得有bug,有的bug 和版本有关的实在太可惜 哈哈,很烦心

https://545c.com/file/24889670-428505918

matlab,最好有安装。实则很多应用工具软件里面,最容易上手的应该就是matlab了。当然以后后来可能有那么小的一段时间大型数据处理时候格式让你几近崩溃的。不过基础就够了的

excel,应该基础的。是大部分人都可以沟通的数据展示传达方式。所以这里也最后写入了exell的

目的:
从网页图片, 多个 循环 基金代码 爬取图上展示的时间序列数据:
做到 数据按交易日对齐,datafram的运用,求并集 交集

一、 找到网址URL:
打开看到这个线Alpha曲线,目前尚未有数据变动是直线。
鼠标在网页上 ,(多年被换不掉的谷歌浏览器步骤)右键-检查打开左侧的面板就可以看到 Network这个标签,然后点击ALL,点击清除,然后点击Alpha线 就重新刷新出来 indicate的元素,点击一个indicate,然后Header,就在General下 找到了URL地址。
根据我写的文字和画的红圈作为重点,点几下琢磨下就会出来。
在这里插入图片描述
在这里插入图片描述

拷贝URL地址:
http://101.201.196.115/fof/v1/indicator?asset_code=206261518029282&indicator_codes=B0202020101%2CB0202020112&appplt=web&Access-Token=3b9aca931706779bb8fxt4917pamlrfo&callback=callback&_1582284545319=
可以自己截取 只要是 有自己 可以要的元素能刷新看到数据就行URL
http://101.201.196.115/fof/v1/indicator?asset_code=206261518029282&indicator_codes=B0202020101%2CB0202020112&appplt=web&Access-Token=3b9aca931706779bb8fxt4917pamlrfo

URL在浏览器打开这就是原始数据了,需要爬下来的在这里插入图片描述
二、可以分析Url其实是需要两个参数一个是assset_code,一个是indicator_codes=‘B0202020101’ 这里代表想要的这个图序指标名称。
assset_code是matlab保存的,load过来。如果没有安装matlab也没有问题,下面就是assset_code打开的形式,这里截了6个。
在这里插入图片描述

三、用python程序爬取joson类型数据序列

最终得到的Excel数据序列
在这里插入图片描述
直接上代码吧

import requests
import jsonpath
import json
import pandas as pd
import numpy as np
import datetime
# yanli qixiancun
# dataFram Union / date /  to_excel / url = "http://101.20

def shengchengUrl(asset_code,indicator_codes):
    # url = 'http://101.201.196.115/fof/v1/indicator?asset_code=206261518029282&indicator_codes=B0202020101%2CB0202020112&appplt=web&Access-Token=3b9aca931706779bb8fxt4917pamlrfo'
    url = "http://101.201.196.115/fof/v1/indicator?asset_code=" + asset_code + "&indicator_codes=" + indicator_codes + "&appplt=web&Access-Token=3b9aca931706779bb8fxt4917pamlrfo"
    return url
def readJsonData(url):
    try:
        headers = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"}
        response = requests.get(url, headers=headers)

        html_str = response.content
        jsonobj = json.loads(html_str)
        citydict = jsonpath.jsonpath(jsonobj, '$..data')
        citylist = citydict[0][u'B0202020112']  # get list
        df = pd.DataFrame(citylist[1:],  columns = ['riqi', 'zhibiao'])

        # df.to_excel(r'changkou.xlsx', sheet_name='Sheet1', na_rep='',
        #             float_format=None, columns=None, header=True, index=True,
        #             index_label=None, startrow=0, startcol=0, engine=None,
        #             merge_cells=True, encoding=None, inf_rep='inf', verbose=True,
        #             freeze_panes=None)
    finally:
        print('ok')
    return df

def dateCut():
    try:
        DateT = loadmat("Date.mat")
        DateT = DateT["date_d_cellstr"]
        sta =  u'20170921'
        # end = u'20200221'  # today = datetime.date.today().strftime('%Y/%m/%d')
        end = u'20200221'
        s = np.where(DateT == sta)
        e = np.where(DateT == end)
        Date = DateT[s[0].max():e[0].max()]
        Date = Date.tolist()
        Date2=Date
        for i in range(0,len(Date)):
            Date2[i]=Date[i][0][0]
        Date2 = pd.DataFrame(Date2, columns=['riqi'])
        # Date['riqi'] = Date['riqi'].apply(lambda x: tuple(x) if type(x) is list else x)
    finally:
        print('dateData ok')
    return Date2

if __name__ == "__main__":
    print ("reading market data")

    from scipy.io import loadmat
    m = loadmat("fund_name_short.mat")
    code=m["fund_name_short"]
    Date2=dateCut()
    dfAll=Date2
    # for i in range(len(code)):
    for i in range(21):
        print   i
        asset_code = code[i][0].item()
        indicator_codes='B0202020112'
        url=shengchengUrl(asset_code, indicator_codes)
        dfdange= readJsonData(url)
        dfAll= pd.merge(dfAll , dfdange, on = 'riqi', how='outer')#

print dfAll

dfAll.to_excel(r'changkou.xlsx', sheet_name='Sheet1', na_rep='',
            float_format=None, columns=None, header=True, index=True,
            index_label=None, startrow=0, startcol=0, engine=None,
            merge_cells=True, encoding=None, inf_rep='inf', verbose=True,
            freeze_panes=None)


如果有人自己程序有问题 必须是 数据结构内含其他结构,没处理统一,所以不能进行数据dataFrame条件合并、取值等操作。
o也一样一样的。。。,经过了n次debug,主要是数据格式处理上,才over,over

代码打包下,不上代码的解释都是传说,没用没用还费时间
https://me.csdn.net/download/zhyl4669

参考 一些例子 部分语法 学习 有需要的看看 更好上手:

https://545c.com/file/24889670-437716638 python 网络爬虫 学爬虫的福音,拿来就能用 实践是最好的老师
https://545c.com/file/24889670-437716627 python 爬虫 学习系列教程 学爬虫的福音,拿来就能用

https://545c.com/file/24889670-428508258 python基础培训.ppt
https://545c.com/file/24889670-428508207 python 核心编程

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值