** Addition ** From "Yahoo! Finance" on March 11, 2015, "[Automatic acquisition (scraping) of Yahoo! Finance publication information is prohibited](http://www.yahoo-help.jp/app/answers/ detail / p / 546 / a_id / 93575) ”and received a request via Qiita management. Therefore, the code below contains a clever bug so that simple actions do not work as intended. ** I do not take any responsibility for any dispute between the user and "Yahoo! Finance" by fixing the bug of this code and using it. ** **
The information you want is
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 = 'Good night' 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
I am always grateful for your help.