OPEN-SOURCE SCRIPT

チャットGPT

62
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup

# 株たんのスクリーニング結果URL(例:200日線以下)
url = "kabutan.jp/warning/?mode=3_1"
r = requests.get(url)
soup = BeautifulSoup(r.text, "html.parser")

# 銘柄コードと企業名を抽出
stocks = []
for link in soup.select("td a[href*='/stock/?code=']"):
code = link['href'].split('=')[-1]
name = link.text.strip()
if code.isdigit():
stocks.append({"code": code, "name": name})

results = []
for stock in stocks[:10]: # ←テスト用に10銘柄まで
ticker = f"{stock['code']}.T"
df = yf.download(ticker, period="1y", interval="1d")

# EMA200
df["EMA200"] = df["Close"].ewm(span=200, adjust=False).mean()
below_ema200 = df["Close"].iloc[-1] < df["EMA200"].iloc[-1]

# 株たんの個別ページからPER・成長率を取得
stock_url = f"kabutan.jp/stock/?code={stock['code']}"
res = requests.get(stock_url)
s = BeautifulSoup(res.text, "html.parser")
try:
per = s.find(text="PER").find_next("td").text
growth = s.find(text="売上高増減率").find_next("td").text
except:
per, growth = "N/A", "N/A"

results.append({
"銘柄コード": stock['code'],
"企業名": stock['name'],
"200EMA以下": below_ema200,
"PER": per,
"売上成長率": growth
})

# 結果をCSV出力
df_result = pd.DataFrame(results)
df_result.to_csv("割安EMA200以下銘柄.csv", index=False, encoding="utf-8-sig")
print(df_result)

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.