爬蟲案例—雪球網(wǎng)行情中心板塊數(shù)據(jù)抓取
雪球網(wǎng)行情中心網(wǎng)址:https://xueqiu.com/hq
目標:市場一覽板塊、熱股榜板塊、新股預告板塊、關注排行榜板塊
import datetime
import requests
headers = {
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
}
# 實例化session對象,進行會話保持
session = requests.Session()
url = 'https://xueqiu.com'
session.get(url, headers=headers)
# 時間戳轉成日期的函數(shù)
def timestamp_to_date(timestamp_data):
timestamp = timestamp_data
# 使用datetime.fromtimestamp()方法將時間戳轉換為datetime對象
datetime_obj = datetime.datetime.fromtimestamp(timestamp)
# 使用strftime()方法將datetime對象格式化為字符串表示的日期
formatted_date = datetime_obj.strftime("%Y-%m-%d")
return formatted_date
# 獲取四大板塊的指數(shù)
def get_four_index():
url = 'https://stock.xueqiu.com/v5/stock/batch/quote.json?symbol=SH000001,SZ399001,SZ399006,SH000688'
res = session.get(url, headers=headers)
items_lst = res.json()['data']['items']
print('市 場 一 覽:')
print()
print('板塊名稱\t指 數(shù)\t漲跌幅\t\t\t總市值')
for item in items_lst:
data_dic = item['quote']
print(data_dic['name'], data_dic['current'], str(data_dic['chg']) + '(' + str(data_dic['percent']) + ')\t\t',
f"{data_dic['market_capital'] / 1000000000000: >.2f}萬億")
print('- ' * 50)
def get_stock(url_dict):
for stock_type in url_lst_dict.keys():
res = session.get(url_dict[stock_type], headers=headers)
res.encoding = res.apparent_encoding
stock_data = res.json()
print(f'熱股榜——{stock_type}:\n')
print('股票代碼\t\t', '股票名稱\t\t\t\t\t', '股票漲跌幅')
for stock in stock_data['data']['items']:
print(f'{stock["code"]:8}\t', f'{stock["name"]:<25}', f'{stock["percent"]:>8}')
print('- ' * 30)
# 定義獲取新股json函數(shù)
def get_json(new_stock_url):
res = session.get(new_stock_url, headers=headers)
new_stock = res.json()
return new_stock
# 定義獲取港股新股函數(shù)
def hk_new_stokc(hk_new_stock_url):
new_stock = get_json(hk_new_stock_url)
print('- ' * 40)
print('港股')
print('新股代碼\t', '新股名稱\t', '上市日期\t', '招股價下限\t', '招股價上限')
new_stock_info = new_stock['data']['items']
for new_stock_item in new_stock_info:
# 上市日期時間戳
list_timestamp = new_stock_item['list_date'] / 1000
print(new_stock_item['symbol'], '\t', new_stock_item['name'], '\t', timestamp_to_date(list_timestamp), '\t',
new_stock_item['issprice_min'], '\t', new_stock_item['issprice_max'])
# 定義滬深新股抓取函數(shù)
def hs_new_stock(hs_new_stock_url):
new_stock = get_json(hs_new_stock_url)
print('新 股 預 告')
print('- ' * 40)
print('滬深')
print('新股發(fā)行數(shù)量:', new_stock['data']['count'])
print('新股代碼\t', '新股名稱\t', '申購代碼\t', '預計發(fā)行量(萬股)\t', '申購上限(萬股)\t', '申購日期\t', '中簽號公布日')
new_stock_info = new_stock['data']['items']
for new_stock_item in new_stock_info:
# 申購日期時間戳
distr_timestamp = new_stock_item['onl_distr_date'] / 1000
# 公布中簽日期時間戳
draw_timestamp = new_stock_item['onl_lotwiner_stpub_date'] / 1000
print(new_stock_item['symbol'], new_stock_item['name'], '\t', new_stock_item['onl_subcode'], '\t',
new_stock_item['actissqty'], '\t\t\t', new_stock_item['onl_sub_maxqty'], '\t\t',
timestamp_to_date(distr_timestamp), '\t', timestamp_to_date(draw_timestamp))
# 定義美股新股抓取函數(shù)
def un_new_stock(un_new_stock_url):
new_stock = get_json(un_new_stock_url)
print('- ' * 50)
print('美股')
print('新股發(fā)行數(shù)量:', new_stock['data']['count'])
print('新股代碼\t', '新股名稱\t', '上市日期\t', '\t股本', '\t招股價下限\t', '招股價上限')
new_stock_info = new_stock['data']['items']
for new_stock_item in new_stock_info:
# 上市日期時間戳
list_timestamp = new_stock_item['list_date'] / 1000
if new_stock_item['shares']:
new_shares = '\t' + str(new_stock_item['shares'] / 10000) + '萬'
else:
new_shares = '\t\t-\t'
if new_stock_item['issprice_min']:
new_min = '\t' + str(new_stock_item['issprice_min']) + '\t'
if new_stock_item['issprice_max']:
new_max = '\t' + str(new_stock_item['issprice_max']) + '\t'
else:
new_max = '\t-'
new_min = '\t-\t'
print(new_stock_item['symbol'], '\t', new_stock_item['name'][:8], '\t', timestamp_to_date(list_timestamp),
new_shares, new_min, new_max)
# 本周排行榜, 本周新增股票,最熱門股票
def get_new_add_stock(new_add_url):
new_add_stock = get_json(new_add_url)
print('- ' * 50)
print('關注排行榜——本周新增')
new_list = new_add_stock['data']['list']
print('股票名稱\t\t股 價\t\t關 注')
for add_stock in new_list:
print(f"{add_stock['name']}\t\t{add_stock['current']}\t\t{int(add_stock['follow7d']):<}")
# 本周排行榜,最熱門股票
def get_hot_stock(new_hot_url):
hot_stock = get_json(new_hot_url)
print('- ' * 50)
print('關注排行榜——最熱門')
hot_lst = hot_stock['data']['list']
print('股票名稱\t\t股 價\t\t關 注')
for hot_stock in hot_lst:
print(f"{hot_stock['name']}\t\t{hot_stock['current']}\t\t{int(hot_stock['follow']):<}")
if __name__ == '__main__':
# 四大板塊信息
get_four_index()
# 熱門股票
url_lst_dict = {'滬深': 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json?page=1&size=9&_type=12&type=12',
'港股': 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json?page=1&size=9&_type=13&type=13',
'美股': 'https://stock.xueqiu.com/v5/stock/hot_stock/list.json?page=1&size=9&_type=11&type=11'}
get_stock(url_lst_dict)
print()
# 滬深新股網(wǎng)址
hs_new_stock_url = 'https://stock.xueqiu.com/v5/stock/preipo/cn/query.json?type=subscribe&order_by=onl_subbeg_date&order=asc&source=new_subscribe&page=1&size=10'
hs_new_stock(hs_new_stock_url)
# 港股新股網(wǎng)址
hk_new_stock_url = 'https://stock.xueqiu.com/v5/stock/preipo/hk/query.json?order=desc&order_by=list_date&type=unlisted&page=1&size=10'
hk_new_stokc(hk_new_stock_url)
# 美股新股網(wǎng)址
un_new_stock_url = 'https://stock.xueqiu.com/v5/stock/preipo/us/list.json?order=desc&order_by=list_date&type=unlisted&page=1&size=10'
un_new_stock(un_new_stock_url)
# 關注排行榜,本周新增
new_add_stock_url = 'https://stock.xueqiu.com/v5/stock/screener/screen.json?page=1&only_count=0&size=10&category=CN&order_by=follow7d&order=desc'
get_new_add_stock(new_add_stock_url)
# 最熱門股票
hot_stock_url = 'https://stock.xueqiu.com/v5/stock/screener/screen.json?page=1&only_count=0&size=10&category=CN&order_by=follow&order=desc'
get_hot_stock(hot_stock_url)
運行結果如下:
文章來源:http://www.zghlxwxcb.cn/news/detail-803267.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-803267.html
到了這里,關于爬蟲案例—雪球網(wǎng)行情中心板塊數(shù)據(jù)抓取的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!