python采集财经数据信息并作可视化~

前言

大家早好、午好、晚好吖~

python采集财经数据信息并作可视化~_第1张图片

开发环境:

  • 解释器版本: python 3.8

  • 代码编辑器: pycharm 2021.2

  • requests: pip install requests

  • pandas: pip install pandas

  • pyecharts: pip install pyecharts

思路:

模拟成 浏览器 向 服务器 发送网络请求

找到数据来源

动态的数据: 如果在网页源代码当中找不到的数据

静态的数据: 如果在网页源代码当中能够找到该数据

实现代码:

  1. 发送请求

  2. 获取数据

  3. 解析数据

  4. 保存数据

代码

import requests     # 发送请求 第三方模块
import csv          # 内置模块
import concurrent.futures

源码、解答、教程加Q裙:261823976 点击蓝字加入【python学习裙】

python采集财经数据信息并作可视化~_第2张图片

f = open('网易财经.csv', mode='a', newline='', encoding='utf-8')
csv_writer = csv.writer(f)
csv_writer.writerow(['代码','名称','价格','涨跌幅','涨跌额','5分钟涨跌额','今开','昨收','最高','最低','成交量','成交额','换手率','量比','委比','振幅','市盈率','流通市值','总市值','每股收益','净利润','主营收'])

headers = {
    # 浏览器的基本信息
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36'
}
for page in range(0, 205):
    url = f'http://quotes.money.163.com/hs/service/diyrank.php?host=http%3A%2F%2Fquotes.money.163.com%2Fhs%2Fservice%2Fdiyrank.php&page={page}&query=STYPE%3AEQA&fields=NO%2CSYMBOL%2CNAME%2CPRICE%2CPERCENT%2CUPDOWN%2CFIVE_MINUTE%2COPEN%2CYESTCLOSE%2CHIGH%2CLOW%2CVOLUME%2CTURNOVER%2CHS%2CLB%2CWB%2CZF%2CPE%2CMCAP%2CTCAP%2CMFSUM%2CMFRATIO.MFRATIO2%2CMFRATIO.MFRATIO10%2CSNAME%2CCODE%2CANNOUNMT%2CUVSNEWS&sort=PERCENT&order=desc&count=24&type=query'
    # 1. 发送请求
    response = requests.get(url, headers=headers)
    # 2. 获取数据
    # .text: 网页源代码, xx源代码  复杂一点
    # .content: 当你的链接里面的数据 是属于 视频/音频/图片
    # .json(): {}/[] 包裹起来的   Python里面的字典类型数据  方便我们接下来解析数据
    json_data = response.json()
    # 3. 解析数据
    for i in range(0, len(json_data['list'])):
        CODE = json_data['list'][i]['CODE']
        NAME = json_data['list'][i]['NAME']
        PRICE = json_data['list'][i]['PRICE']
        PERCENT = json_data['list'][i]['PERCENT']
        UPDOWN = json_data['list'][i]['UPDOWN']
        FIVE_MINUTE = json_data['list'][i]['FIVE_MINUTE']
        OPEN = json_data['list'][i]['OPEN']
        YESTCLOSE = json_data['list'][i]['YESTCLOSE']
        HIGH = json_data['list'][i]['HIGH']
        LOW = json_data['list'][i]['LOW']
        VOLUME = json_data['list'][i]['VOLUME']
        TURNOVER = json_data['list'][i]['TURNOVER']
        try:
            HS = json_data['list'][i]['HS']
        except:
            HS = ''

        try:
            LB = json_data['list'][i]['LB']
        except:
            LB = ''
        WB = json_data['list'][i]['WB']
        ZF = json_data['list'][i]['ZF']
        try:
            PE = json_data['list'][i]['PE']
        except:
            PE = ''
        try:
            MCAP = json_data['list'][i]['MCAP']
        except:
            MCAP = ''
        TCAP = json_data['list'][i]['TCAP']
        MFSUM = json_data['list'][i]['MFSUM']
        MFRATIO2 = json_data['list'][i]['MFRATIO']['MFRATIO2']
        MFRATIO10 = json_data['list'][i]['MFRATIO']['MFRATIO10']
        print(CODE, NAME, PRICE, PERCENT, UPDOWN, FIVE_MINUTE, OPEN, YESTCLOSE, HIGH, LOW, VOLUME, TURNOVER, HS, LB, WB, ZF, PE, MCAP, TCAP, MFSUM, MFRATIO2, MFRATIO10)
        # 4. 保存数据
        csv_writer.writerow([CODE, NAME, PRICE, PERCENT, UPDOWN, FIVE_MINUTE, OPEN, YESTCLOSE, HIGH, LOW, VOLUME, TURNOVER, HS, LB, WB, ZF, PE, MCAP, TCAP, MFSUM, MFRATIO2, MFRATIO10])

可视化

from pyecharts.charts import Bar
from pyecharts import options as opts
import pandas as pd


df = pd.read_csv('网易财经.csv', engine="python", encoding='utf-8')
x = list(df['名称'].values)
y = df['成交量'].values.tolist()
c = (
    Bar()
    .add_xaxis(x[:10])
    .add_yaxis('成交量情况', y[:10])
    .set_global_opts(
        title_opts=opts.TitleOpts(title='成交量图表'),
        datazoom_opts=opts.DataZoomOpts()
    )
)
c.render('成交量图表.html')

python采集财经数据信息并作可视化~_第3张图片

尾语

好了,我的这篇文章写到这里就结束啦!

有更多建议或问题可以评论区或私信我哦!一起加油努力叭(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

你可能感兴趣的:(python基础,数据分析入门,python,开发语言,pycharm)