前言
嗨嘍!大家好呀六荒,這里是魔王~
雪球,聰明的投資者都在這里 - 4300萬投資者都在用的投資社區(qū),
滬深港美全球市場實時行情,股票基金債券免費資訊,與投資高手實戰(zhàn)交流缨睡。
模塊使用
- requests >>> pip install requests (數(shù)據(jù)請求 第三方模塊)
- re # 正則表達式 去匹配提取數(shù)據(jù)
- json
- pandas
- pyecharts
開發(fā)環(huán)境
- Python 3.8 解釋器
- Pycharm 2021.2 版本
代碼實現(xiàn)步驟
- 發(fā)送請求 訪問網(wǎng)站
- 獲取數(shù)據(jù)
- 解析數(shù)據(jù)(提取數(shù)據(jù))
- 保存數(shù)據(jù)
- 做柱狀圖 簡單的可視化
代碼
# 1. 發(fā)送請求 訪問網(wǎng)站
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
}
url = 'https://xueqiu.com/service/v5/stock/screener/quote/list?page=1&size=30&order=desc&order_by=amount&exchange=CN&market=CN&type=sha&_=1641730868838'
response = requests.get(url=url, headers=headers)
# 2. 獲取數(shù)據(jù)
json_data = response.json()
# 3. 數(shù)據(jù)解析(篩選數(shù)據(jù))
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)
4. 保存地址
file = open('data2.csv', mode='a', encoding='utf-8', newline='')
csv_write = csv.DictWriter(file, fieldnames=['股票代碼','股票名稱','當前價','漲跌額','漲跌幅','年初至今','成交量','成交額','換手率','市盈率(TTM)','股息率','市值'])
csv_write.writeheader()
運行效果
數(shù)據(jù)可視化
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(df2['股票名稱'].values.tolist())
.add_yaxis("股票成交量情況", df2['成交量'].values.tolist())
.set_global_opts(
title_opts=opts.TitleOpts(title="成交量圖表 - Volume chart"),
datazoom_opts=opts.DataZoomOpts(),
)
.render("data.html")
)
print('數(shù)據(jù)可視化結果完成,請在當前目錄下查找打開 data.html 文件!')
尾語
好了,我的這篇文章寫到這里就結束啦幅虑!
有更多建議或問題可以評論區(qū)或私信我哦!一起加油努力叭(? ?_?)?
喜歡就關注一下博主顾犹,或點贊收藏評論一下我的文章叭5光帧0!