嗨嗨,大家好下午好,我是小圆 ~
今天给大家分享一下,如何用python采集财经数据信息并作可视化
解释器版本: python 3.8
代码编辑器: pycharm 2021.2
requests: pip install requests
pandas: pip install pandas
pyecharts: pip install pyecharts
模拟成 浏览器 向 服务器 发送网络请求
动态的数据: 如果在网页源代码当中找不到的数据
静态的数据: 如果在网页源代码当中能够找到该数据
import requests # 发送请求 第三方模块
import csv # 内置模块
import concurrent.futures
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的问题,可以在评论区留言或者私信我哦
觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/