您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Python怎么爬取股票交易數(shù)據(jù)并可視化展示”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
解釋器版本: python 3.8
代碼編輯器: pycharm 2021.2
requests: pip install requests
csv
1.確定url地址(鏈接地址)
2.發(fā)送網(wǎng)絡(luò)請求
3.數(shù)據(jù)解析(篩選數(shù)據(jù))
4.數(shù)據(jù)的保存(數(shù)據(jù)庫(mysql\mongodb\redis), 本地文件)
打開開發(fā)者工具,搜索關(guān)鍵字,找到正確url
import requests # 發(fā)送網(wǎng)絡(luò)請求 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, '當(dāng)前價': data3, '漲跌額': data4, '漲跌幅': data5, '年初至今': data6, '成交量': data7, '成交額': data8, '換手率': data9, '市盈率(TTM)': data10, '股息率': data11, '市值': data12, } csv_write.writerow(data_dict)
對比1、2、3頁數(shù)據(jù)url,找到規(guī)律
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=['股票代碼','股票名稱','當(dāng)前價','漲跌額','漲跌幅','年初至今','成交量','成交額','換手率','市盈率(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('數(shù)據(jù)可視化結(jié)果完成,請在當(dāng)前目錄下查找打開 data.html 文件!')
“Python怎么爬取股票交易數(shù)據(jù)并可視化展示”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。