MT4用にPython経済指標をスクレイピングして取得し整形して返却するWEBアプリケーション
MT4で指標時に停止する機能を作ろうと思ったのですが、MT4からスクレイピングしてもかなりの手間がかかるのでPythonで取得するアプリを作ります。
形式は、指標で改行するのでMT4で使いまわすことが出来て便利かも。
外部でスクレイピング装置を作ってサーバーに設置しておくとMT4でも手軽にPythonが使えますね。
URL
https://us-central1-optical-legend-324601.cloudfunctions.net/sihyou
GCPのCloud Functionsを使う
いつものやつです。無料枠で使えます。
Pythonソースコード
main.py
import functions_framework
import requests
from bs4 import BeautifulSoup
import re
# 国の名前を3文字の国コードに変換するための辞書
COUNTRY_CODES = {
"南アフリカ": "ZAF",
"ユーロ": "EUR",
"イギリス": "GBR",
"カナダ": "CAN",
"米国": "USA",
"フランス": "FRA",
"トルコ": "TUR",
"ドイツ": "DEU",
"ニュージーランド": "NZL",
"シンガポール": "SGP",
"日本": "JPN",
"オーストラリア": "AUS",
"香港": "HKG",
"中国": "CHN",
"スイス": "CHE",
"ブラジル": "BRA",
"ロシア": "RUS",
"インド": "IND",
"韓国": "KOR",
"メキシコ": "MEX",
"サウジアラビア": "SAU",
"アルゼンチン": "ARG",
"インドネシア": "IDN",
"イタリア": "ITA",
"スペイン": "ESP",
"オランダ": "NLD",
"スウェーデン": "SWE",
"ノルウェー": "NOR",
"ポーランド": "POL",
"ベルギー": "BEL",
"オーストリア": "AUT",
"デンマーク": "DNK",
"フィンランド": "FIN",
"ギリシャ": "GRC",
"アイルランド": "IRL",
"ポルトガル": "PRT",
"台湾": "TWN",
"タイ": "THA",
"マレーシア": "MYS",
"フィリピン": "PHL",
"チリ": "CHL",
"コロンビア": "COL",
"ペルー": "PER",
"ベネズエラ": "VEN",
"エジプト": "EGY",
"イスラエル": "ISR",
"クウェート": "KWT",
"アラブ首長国連邦": "ARE",
"カタール": "QAT",
"バーレーン": "BHR",
"オマーン": "OMN"
}
def get_sihyou():
url = "https://www.gaikaex.com/gaikaex/mark/calendar/"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
response = requests.get(url, headers=headers)
response.raise_for_status() # Check if the request was successful
soup = BeautifulSoup(response.content, 'html.parser')
result_str = "日付, 時間, 国, 重要度, 指標名\n" # Header
table = soup.select_one("#contentsAreaInner > div.tableA01 > table")
if not table:
return "Error: Table not found"
rows = table.select("tbody > tr")
current_date = None # <-- ここで初期化
for row in rows:
tds = row.find_all('td')
if tds[0].has_attr('rowspan'):
current_date = tds[0].text.strip()
tds = tds[1:] # Remove the date cell
else:
current_date = current_date # Use the previously found date
data = []
column_names = ['時間', '国', '指標名', '重要度']
for idx, td in enumerate(tds):
if idx < len(column_names):
if column_names[idx] == '国':
country_name = td.text.strip() if td.text else ""
# 国の名前を3文字の国コードに変換
country_code = COUNTRY_CODES.get(country_name, country_name)
data.append(country_code)
elif column_names[idx] == '重要度':
stars_count = len(re.findall("★", td.text)) # "★"の数をカウント
data.append(str(stars_count)) # "★"の数だけを保存
else:
data.append(td.text.strip() if td.text else "")
# 重要度が0でない場合のみ、結果の文字列に追加
if stars_count != 0:
result_str += f"{current_date}, {data[0]}, {data[1]}, {data[3]}, {data[2]}\n"
return result_str
@functions_framework.http
def scrape_calendar(request):
data = get_sihyou()
return (data, 200, {
"Content-Type": "text/plain; charset=utf-8"
})
requirements .txt
functions-framework==3.*
requests
beautifulsoup4
ここから先は
0字
このマガジンで読み放題です。
FX自動売買開発入門サンプルコードセット
10,000円
EA開発者のためのサンプルコード集
この記事が気に入ったらサポートをしてみませんか?