本篇内容介绍了“Python怎么爬取股票交易数据并可视化展示”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
解释器版本: python 3.8
代码编辑器: pycharm 2021.2
requests: pip install requests
csv
1.确定url地址(链接地址)
2.发送网络请求
3.数据解析(筛选数据)
4.数据的保存(数据库(mysql\mongodb\redis), 本地文件)
打开开发者工具,搜索关键字,找到正确url
import requests # 发送网络请求 import csv
url = f'https://xueqiu.com/service/v5/stock/screener/quote/list?page=1&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1637908787379' # 伪装 headers = { # 浏览器伪装 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36' } response = requests.get(url, headers=headers) json_data = response.json()
data_list = json_data['data']['list'] for data in data_list: data1 = data['symbol'] data2 = data['name'] data3 = data['current'] data4 = data['chg'] data5 = data['percent'] data6 = data['current_year_percent'] data7 = data['volume'] data8 = data['amount'] data9 = data['turnover_rate'] data10 = data['pe_ttm'] data11 = data['dividend_yield'] data12 = data['market_capital'] print(data1, data2, data3, data4, data5, data6, data7, data8, data9, data10, data11, data12) data_dict = { '股票代码': data1, '股票名称': data2, '当前价': data3, '涨跌额': data4, '涨跌幅': data5, '年初至今': data6, '成交量': data7, '成交额': data8, '换手率': data9, '市盈率(TTM)': data10, '股息率': data11, '市值': data12, } csv_write.writerow(data_dict)
对比1、2、3页数据url,找到规律
for page in range(1, 56): url = f'https://xueqiu.com/service/v5/stock/screener/quote/list?page={page}&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1637908787379'
file = open('data2.csv', mode='a', encoding='utf-8', newline='') csv_write = csv.DictWriter(file, fieldnames=['股票代码','股票名称','当前价','涨跌额','涨跌幅','年初至今','成交量','成交额','换手率','市盈率(TTM)','股息率','市值']) csv_write.writeheader() file.close()
import pandas as pd from pyecharts import options as opts from pyecharts.charts import Bar
data_df = pd.read_csv('data2.csv') df = data_df.dropna() df1 = df[['股票名称', '成交量']] df2 = df1.iloc[:20] print(df2['股票名称'].values) print(df2['成交量'].values)
c = ( Bar() .add_xaxis(list(df2['股票名称'])) .add_yaxis("股票成交量情况", list(df2['成交量'])) .set_global_opts( title_opts=opts.TitleOpts(title="成交量图表 - Volume chart"), datazoom_opts=opts.DataZoomOpts(), ) .render("data.html") ) print('数据可视化结果完成,请在当前目录下查找打开 data.html 文件!')
“Python怎么爬取股票交易数据并可视化展示”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。