In [1]:
from bs4 import BeautifulSoup
import re
from urllib import urlopen
from datetime import datetime
from PIL import Image
from cStringIO import StringIO

In [2]:
def parse_yahoo_jp_stock_diagram(stock_id):
    diag_url = u'http://chart.yahoo.co.jp/?code={}.T&tm=1y&type=c&log=off&size=n&over=m65,m130,s&add=v&comp'
    try:
        img = Image.open(StringIO(urlopen(diag_url.format(stock_id)).read()))
    except:
        return None
    if img.height < 250:
        return None
    return img

In [3]:
def parse_yahoo_jp_finance(url):
    if url.endswith('.vip'):
        raise ValueError('Cannot parse vip-only pages.')
    html = urlopen(url).read().decode('utf8')
    soup = BeautifulSoup(html, 'html.parser')
    content = soup.find_all('div', class_='ymuiContainerNopad clearFix s170')[0]
    content = re.sub('\<\/ *?[Bb][Rr]\>', '', re.sub('\<[Bb][Rr]\>', '\n', str(content)))
    content = BeautifulSoup(content, 'html.parser').children.next()
    
    data = {}
    for x in content.find_all('div'):
        x.extract()
    for x in content.find_all('ul'):
        x.extract()
    data['html'] = content
    data['title'] = soup.find_all('h1')[0].text
    data['stock_id'] = [x.text for x in content.find_all('a')]
    data['stock_id'].extend(re.findall('[0-9]{4}/TW', content.text))
    data['stock_id'].extend(re.findall('[0-9]{4}/TW', content.text))
    data['stock_id'].extend(re.findall('[0-9]{4}/TW', content.text))
    data['text'] = content.text
    time_str = soup.find_all('p', class_='ymuiDate')[0].text
    time_str = re.findall(u'([0-9]+)時([0-9]+)分', time_str)[0]
    date_str = re.findall('([12][09][019][0-9])([01][0-9])([0-3][0-9])', url)[0]
    data['date'] = datetime.strptime('-'.join(date_str + time_str), '%Y-%m-%d-%H-%M')
    return data

In [4]:
parse_yahoo_jp_stock_diagram('1414')


Out[4]:

In [5]:
url = 'http://news.finance.yahoo.co.jp/detail/20161129-00000194-stkms-stocks'
d = parse_yahoo_jp_finance(url)
print d['date']
print d['title']
print d['stock_id']
print d['text']


2016-11-29 15:37:00
本日の【ゴールデンクロス/デッドクロス】引け GC= 97 銘柄 DC= 35 銘柄 (11月29日)
[u'9432', u'2914', u'9531', u'1878', u'3625', u'8984', u'7240', u'3923', u'9509', u'6965', u'6258', u'6727', u'3101', u'6428', u'6324', u'6134', u'4924', u'4549', u'8973', u'3309', u'5341', u'8088', u'1414', u'3087', u'3333', u'4842', u'3918', u'3909', u'3222', u'3237', u'7780', u'2453', u'3031', u'6862', u'2734', u'7269', u'4523', u'7818', u'7294', u'2698', u'4514', u'6071', u'6013', u'6469', u'9837', u'8887', u'1540', u'3935', u'3418', u'7921']



■11月29日の終値ベースで、5日と25日の移動平均線がゴールデンクロス/デッドクロスした銘柄

●ゴールデンクロス銘柄(市場人気順) ★上昇トレンドに転換した可能性が高い銘柄

    銘柄名          株価  前日比 (比率)   5日移動平均 25日移動平均
<9432> 日本電信電話       4610    -3 ( -0.1%)     4538.8    4502.8
<2914> JT           3948    +37 ( +1.0%)     3896.0    3874.6
<9531> 東京ガス         488.2   -5.7 ( -1.2%)     480.8    479.2
<1878> 大東建託         17545    -5 ( -0.0%)    17212.0   17171.0
<3625> テックファム       1912    -26 ( -1.3%)     1895.2    1882.8
<8984> ハウスリート      284500   +3300 ( +1.2%)    281940.0   281584.0
<7240> NOK          2287    -46 ( -2.0%)     2262.8    2259.0
<3923> ラクス          1473    +78 ( +5.6%)     1374.2    1336.7
<9509> 北海道電力         824    -1 ( -0.1%)     813.0    810.8
<6965> 浜松ホトニクス      3065    -5 ( -0.2%)     3029.2    3020.0
<6258> 平田機工         7550    -20 ( -0.3%)     7334.0    7301.2
<6727> ワコム           308    +3 ( +1.0%)     300.4    297.1
<3101> 東洋紡           172     0 ( 0.0%)     169.2    167.8
<6428> オーイズミ         740    -24 ( -3.1%)     718.6    706.6
<6324> ハーモニック       2936    -13 ( -0.4%)     2865.4    2839.5
<6134> 富士機械製造       1303    +3 ( +0.2%)     1287.8    1283.6
<4924> シーズHD        2954    +6 ( +0.2%)     2932.6    2928.0
<4549> 栄研化学         2965   +151 ( +5.4%)     2851.8    2841.8
<8973> 積ハSIレジ      119500   -100 ( -0.1%)    118140.0   118132.0
<3309> 積水ハウスR      143000   +1200 ( +0.9%)    142060.0   141968.0
<5341> アサヒ衛陶         100    +8 ( +8.7%)      94.6     94.4
<8088> 岩谷産業          617    -2 ( -0.3%)     608.0    607.0
<1414> ショーボンド       4995    +20 ( +0.4%)     4965.0    4955.6
<3087> ドトル日レス       2128    -5 ( -0.2%)     2099.8    2096.6
<3333> あさひ          1254    +11 ( +0.9%)     1244.2    1241.8
<4842> USEN          338    +3 ( +0.9%)     328.8    328.7
<3918> PCIホールディングス  2416     0 ( 0.0%)     2389.6    2374.4
<3909> ショーケース       1212    -33 ( -2.7%)     1174.8    1170.4
<3222> USMH          951    -4 ( -0.4%)     943.8    939.8
<3237> イントランス        187    -5 ( -2.6%)     185.0    183.8
<7780> メニコン         2951    +13 ( +0.4%)     2905.8    2880.0
<2453> JBR           255    +3 ( +1.2%)     248.0    243.2
<3031> ラクーン          507     0 ( 0.0%)     504.0    503.4
<6862> ミナトホールディングス   93    +3 ( +3.3%)      87.6     86.4
<2734> サーラコーポレーション   631    +17 ( +2.8%)     624.2    622.4

など、97銘柄

●デッドクロス銘柄(市場人気順) ★下降トレンドに転換した可能性が高い銘柄

    銘柄名          株価  前日比 (比率)   5日移動平均 25日移動平均
<7269> スズキ          3631    -63 ( -1.7%)     3737.8    3753.9
<4523> エーザイ         6611    -47 ( -0.7%)     6707.6    6715.1
<7818> トランザクション     1802    +11 ( +0.6%)     1873.6    1906.4
<7294> ヨロズ          1590    +2 ( +0.1%)     1608.2    1615.7
<2698> キャンドゥ        1801    +4 ( +0.2%)     1820.2    1821.0
<4514> あすか製薬        1713    -2 ( -0.1%)     1746.2    1757.0
<6071> IBJ           736    -12 ( -1.6%)     740.8    746.9
<6013> タクマ           944    -15 ( -1.6%)     959.6    959.7
<6469> 放電精密加工研究所    1120    -17 ( -1.5%)     1155.4    1163.1
<9837> モリト           800    -2 ( -0.3%)     805.0    805.2
<8887> リベレステ         771    -4 ( -0.5%)     789.6    790.5
<1540> 純金信託         4170     0 ( 0.0%)     4178.0    4179.4
<3935> エディア         1894    -66 ( -3.4%)     1924.0    1936.8
<3418> バルニバービ       3000    -70 ( -2.3%)     3125.0    3157.0
<7921> 宝印刷          1410    -1 ( -0.1%)     1434.0    1436.7

など、35銘柄

提供:株式分析ソフト『ストックボード』から転載。



In [ ]: