Pythonによる新浪财经の株価データ取得方法

新浪财经からの株価履歴データ取得

新浪财经のURL構造(例: http://market.finance.sina.com.cn/transHis.php?symbol=sz000001&date=2021-04-27&page=60)から株価データを取得します。日付と銘柄コードを入力し、全取引データをCSVに保存後、当日の最高値・最安値・平均値を計算します。

必要なライブラリ

import requests
import time
from bs4 import BeautifulSoup
import csv
import os
import pandas as pd

USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"
stock_records = []  # 株価データ格納用リスト

メイン処理

def execute_scraping():
    stock_code = input("銘柄コードを入力(例: sz000001): ")
    target_date = input("日付を入力(形式: 2021-04-27): ")
    
    for page_num in range(1, 100):
        status = fetch_page(stock_code, target_date, page_num)
        time.sleep(3)  # リクエスト間隔
        if status == 0:  # 最終ページ到達
            break
    
    save_to_csv(stock_records, stock_code, target_date)
    analyze_with_pandas(stock_code, target_date)

ページ取得処理

def fetch_page(stock_id, trade_date, page_index):
    url = f"https://market.finance.sina.com.cn/transHis.php?symbol={stock_id}&date={trade_date}&page={page_index}"
    print(f"アクセスURL: {url}")
    
    try:
        response = requests.get(url, headers={"User-Agent": USER_AGENT})
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        return parse_content(response.text, page_index)
    except Exception as e:
        print(f"エラー発生: {e}")
        return 0

HTML解析処理

def parse_content(html_content, current_page):
    soup = BeautifulSoup(html_content, 'lxml')
    
    if soup.tbody.string is not None:  # データなし判定
        print("最終ページ到達")
        return 0
    
    for row in soup.tbody.find_all('tr'):
        time_cell = row.select('th')[0].get_text()
        data_cells = [cell.get_text() for cell in row.select('td')]
        amount_value = float(data_cells[3].replace(',', ''))
        record = [time_cell, *data_cells[:3], amount_value, row.select('th')[1].contents[0].get_text()]
        stock_records.append(record)
    
    return 1

CSV保存処理

def save_to_csv(data_list, stock_id, trade_date):
    filename = f"sina_{stock_id}_{trade_date}.csv"
    with open(filename, 'w', newline='', encoding='utf-8-sig') as file:
        writer = csv.writer(file)
        writer.writerow(['取引時間', '価格', '変動幅', '出来高(手)', '取引金額', '属性'])
        writer.writerows(data_list)
    print(f"{filename} に保存完了")

データ分析処理

def analyze_with_pandas(stock_id, trade_date):
    filename = f"sina_{stock_id}_{trade_date}.csv"
    if not os.path.exists(filename):
        print("ファイルが存在しません")
        return
    
    data = pd.read_csv(filename, encoding='utf-8-sig')
    metrics = ['価格', '出来高(手)', '取引金額']
    result_df = pd.DataFrame({
        '最大値': [data[col].max() for col in metrics],
        '最小値': [data[col].min() for col in metrics],
        '平均値': [data[col].mean() for col in metrics]
    }, index=metrics)
    
    print(result_df)

タグ: Python Webスクレイピング BeautifulSoup 新浪财经 株価分析

8月21日 07:08 投稿