Skip to content

Instantly share code, notes, and snippets.

@senseilearning
Created September 29, 2018 14:30
Show Gist options
  • Save senseilearning/f830d8e54c9ca2009a4282246e0faa45 to your computer and use it in GitHub Desktop.
Save senseilearning/f830d8e54c9ca2009a4282246e0faa45 to your computer and use it in GitHub Desktop.
日本経済新聞にアクセスを行いその時の日経平均株価を取得
# coding: UTF-8
import urllib.request, urllib.error
from bs4 import BeautifulSoup
# アクセスするURL
url = "http://www.nikkei.com/markets/kabu/"
# URLにアクセスする htmlが帰ってくる → <html><head><title>経済、株価、ビジネス、政治のニュース:日経電子版</title></head><body....
html = urllib.request.urlopen(url)
# htmlをBeautifulSoupで扱う
soup = BeautifulSoup(html, "html.parser")
# span要素全てを摘出する→全てのspan要素が配列に入ってかえされます→[<span class="m-wficon triDown"></span>, <span class="l-h...
span = soup.find_all("span")
# print時のエラーとならないように最初に宣言しておきます。
nikkei_heikin = ""
# for分で全てのspan要素の中からClass="mkc-stock_prices"となっている物を探します
for tag in span:
# classの設定がされていない要素は、tag.get("class").pop(0)を行うことのできないでエラーとなるため、tryでエラーを回避する
try:
# tagの中からclass="n"のnの文字列を摘出します。複数classが設定されている場合があるので
# get関数では配列で帰ってくる。そのため配列の関数pop(0)により、配列の一番最初を摘出する
# <span class="hoge" class="foo"> → ["hoge","foo"] → hoge
string_ = tag.get("class").pop(0)
# 摘出したclassの文字列にmkc-stock_pricesと設定されているかを調べます
if string_ in "mkc-stock_prices":
# mkc-stock_pricesが設定されているのでtagで囲まれた文字列を.stringであぶり出します
nikkei_heikin = tag.string
# 摘出が完了したのでfor分を抜けます
break
except:
# パス→何も処理を行わない
pass
# 摘出した日経平均株価を出力します。
print(nikkei_heikin)
@senseilearning
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment