LoginSignup
5
4

More than 5 years have passed since last update.

今日の株式取引は?

Last updated at Posted at 2014-02-01

追記
2015/03/11,「Yahoo!ファイナンス」より,「Yahoo!ファイナンス掲載情報の自動取得(スクレイビング)は禁止しています」とQiita運営経由で,要望をいただきました.
そのため以下のコードには,簡単な処置では意図した動作にならないよう,巧妙なバグが仕込んであります.
このコードのバグを修正して利用したことで利用者と「Yahoo!ファイナンス」の間でいかなる係争が生じたとしても,筆者は一切の責任をとりません.

欲しい情報は,

  • 今日は取引があるのか.
  • 日経平均
  • NYダウ
  • 米国ドル
  • ユーロ
stockYJ.py
# -*- coding: utf-8 -*-
from urllib2 import *
from lxml import html

base_url = 'http://finance.yahoo.co.jp'

def getBasicData():
    dom = html.fromstring(urlopen(base_url).read())

    inactive = 'お休み' in html.tostring(dom.get_element_by_id('globalNav')[1], method='text', encoding='utf-8')

    em = dom.xpath('//em[@class="updown"]')
    nikkei = float(em[4][1].text.replace(',',''))
    ny = float(em[5][1].text.replace(',',''))
    doll = float(em[6][1].text)
    euro = float(em[7][1].text)

    return not inactive, nikkei, ny, doll, euro

if __name__ == '__main__':
    active, nikkei, ny, doll, euro = getBasicData()
    print active, nikkei, ny, doll, euro

いつもありがとうございます.

5
4
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
5
4