LoginSignup
2
8

More than 3 years have passed since last update.

Pythonでスクレイピング(BeautifulSoup)

Posted at

python でスクレイピングをやったので、メモとして残しておきます。
(Pythonによるスクレイピング&機械学習を参考にやりました)

BeautifulSoup

BeautifulSoup : Pythonでスクレイピングをするのに欠かせない優秀なライブラリ。
HTMLやXMLを解析するライブラリです。

BeautifulSoup のインストール

$ pip3 install beautifulsoup4

pip3は、pipのpython3版です

Yahoo!ファイナンスの為替情報取得方法

sample.py
from bs4 import BeautifulSoup
import urllib.request as req

url = "https://stocks.finance.yahoo.co.jp/stocks/detail/?code=usdjpy"
res = req.urlopen(url)

soup = BeautifulSoup(res,"html.parser")

price = soup.select_one(".stoksPrice").string
print("usd/jpy",price)

POINT

HTMLを解析したいと思った時は、BeautifulSoupが便利みたいです!
構造が複雑でも、必要なデータ・欲しいデータを簡単に抽出できます。
また、CSSセレクタを利用した抽出にも対応しているので、Webブラウザーで構造を確認すれば、任意の要素を手軽に取り出せます。

参考文献

Pythonによるスクレイピング&機械学習

2
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
8