【中級編】某ウルフさん銘柄のリストを作成するPythonコード|イザナミ豆知識

イザナミで某ウルフさん銘柄に仕掛ける/避ける用のCSVファイルを作成するPythonコードつくってみました。


データ取得先

「最近のウルフ村田注目株」というページがありましたのでこちらから情報を取得してみます。

コード

wolf_stock.csvというファイルで出力します。

import requests
from lxml import html
import csv

# URL of the page to scrape
url = 'https://wolf-fun.secret.jp/?最近のウルフ注目株'

# Send a GET request to the URL
response = requests.get(url)

# Parse the HTML content of the page
tree = html.fromstring(response.content)

# Use the XPath to select the table
table = tree.xpath('//*[@id="contents"]/div[1]/table')[0]

# Open a new CSV file to write into
with open('wolf_stock.csv', 'w', newline='', encoding='cp932') as csvfile:
    csvwriter = csv.writer(csvfile)
    # Write the header
    csvwriter.writerow(['code', 'wolf flag', 'name'])

    # Assuming the stock information is in the 4rd column (index3)
    for row in table.xpath('.//tr'):
        # Extract text from the anchor tag within the stock information cell
        stock_info_cells = row.xpath('.//td[3]//a/text()')
        if stock_info_cells:
            stock_info = stock_info_cells[0].strip()
            # Split the stock code and name
            stock_code, company_name = stock_info.split(maxsplit=1)
            # Write to CSV
            csvwriter.writerow([stock_code, 1,company_name])
            print(f"{stock_code},1,{company_name}")  # Optional: print to console

必要なライブラリ

requests / lxml

pip install requests
pip install lxml

イザナミでの利用方法

環境データの設定

分類:銘柄別
時系列:通年(共通)
データなし時の扱い:数値のゼロ

売買ルールでの設定例

0と同じ = ウルフ銘柄なら仕掛けない

ご注意

データ取得元にスクレイピング可否についての記述はありませんでしたが、問題がありましたらこのnoteの公開を停止します。

データ取得元の構造が変わった場合は取得ができなくなります。

参考(環境データのマニュアル)

お問合せ先

有限会社 ツクヨミ
URL https://www.izanami.jp
Mail support@izanami.jp
🔎イザナミnote内検索

この記事が気に入ったらサポートをしてみませんか?