Yahoo の USD/JPY の過去データを取り込むプログラムを作成中。
本日学んだことは以下の通り。
- Requests のインストール
- Beautifulsoup のインストール
- Webページの読み込み
- テーブルの探索とデータの取得
- 文字列の置き換え
■ソースコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gspread | |
import datetime | |
import csv | |
import requests | |
from bs4 import BeautifulSoup | |
# Web ページの読み込み | |
r = requests.get('https://info.finance.yahoo.co.jp/history/?code=usdjpy') | |
# コンテンツの解析 | |
s = BeautifulSoup(r.content, "html.parser") | |
# テーブルの探索 | |
tr = s.find_all('tr') | |
# テーブル内データの取得 | |
for td in tr: | |
i = td.find_all('td') | |
for l in i: | |
if'<td>' in str(l): | |
lst = str(l) | |
# タグの消去 | |
lst = lst.replace('<td>', '') | |
lst = lst.replace('</td>', '') | |
print(lst) |