#実現したいこと
nokogiriを使って数値を抽出しようとしているのですが実行するとNaNとだけ出力されてしまいます。Webページは血圧計の結果を送ると表に記録されます。13日の値を取り出そうとしています
###該当のソースコード
ruby
1#!/usr/bin/ruby 2require 'mechanize' 3agent = Mechanize.new 4agent.user_agent_alias = 'Windows Mozilla' 5url = 'https://www.watashi-move.jp/wl/mydata/sphygmomanometer.php?targetDate=2016/07/01' 6page = agent.get(url) 7mypage = page.form_with(id: 'form1') do |form| 8form.loginid = '*' 9form.password = '*' 10end.submit 11 12require 'nokogiri' 13require 'open-uri' 14url = 'https://www.watashi-move.jp/wl/mydata/sphygmomanometer.php?targetDate=2016/07/01' 15user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36' 16charset = nil 17html = open(url, "User-Agent" => user_agent) do |f| 18charset = f.charset 19f.read 20doc = Nokogiri::HTML.parse(html, nil, charset) 21doc.xpath('//tr[@class="day13"]/td[@class="pressureMin"]').text 22puts doc.xpath('//tr[@class="day13"]/td[@class="pressureMin"]').text 23end
###試したこと
Xpathの部分をdoc.xpath('//*[@class="datas"]/table[@class="datalist"]/tr[@id=listTr13]/td[@class="pressureMin"]/span')など試しましたがだめでした
###HTML
文字数の関係で13日以外削りました
w3.org/TR/xhtml1/DTD/xhtml1
1<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><meta http-equiv="Content-Style-Type" content="text/css" /><meta http-equiv="Content-Script-Type" content="text/javascript" /><meta name="robots" content="noindex,nofollow" /> 4<title>閲覧</title> 5<link rel="shortcut icon" href="/wl/images/common/favicon.ico" type="image/x-icon" /><link rel="stylesheet" type="text/css" href="/wl/css/common/reset_1610261.css" /><link rel="stylesheet" type="text/css" href="/wl/css/common/base_1610261.css" /><link rel="stylesheet" type="text/css" href="/wl/css/jcal_1610261.css" /><link rel="stylesheet" type="text/css" href="/wl/css/mydata_edit_common_1610261.css" /><link rel="stylesheet" type="text/css" href="/wl/css/mydata_sphygmomanometer_1610261.css" /><link rel="stylesheet" type="text/css" href="/wl/css/mydata_edit_ec_1610261.css" /> 6<script type="text/javascript" src="/wl/js/common/jquery-1.4.2.min.js" charset="utf-8"></script> 7<script type="text/javascript" src="/wl/js/common/base_1610261.js" charset="utf-8"></script><script type="text/javascript" src="/wl/js/common/action_1610261.js" charset="utf-8"></script><script type="text/javascript" src="/wl/js/jcal_1610261.js" charset="utf-8"></script><script type="text/javascript" src="/wl/js/mydata_edit_cmn_1610261.js" charset="utf-8"></script><script type="text/javascript" src="/wl/js/config/pc_msg_ja_1610261.js" charset="utf-8"></script><script type="text/javascript" src="/wl/js/config/pc_mydata_label_ja_1610261.js" charset="utf-8"></script> 8//<![CDATA[ 9var cookieOptions = {}; 10cookieOptions.expires = 9999;cookieOptions.path = "/";cookieOptions.domain = document.domain; 11if ($.cookie("isBroadFont") == '1'){ 12$("head").append('<link rel="stylesheet" href="/member/css/common/meiryo_1610261.css" type="text/css" /> '); 13} else if($.cookie("isBroadFont") == '2'){ 14$("head").append('<link rel="stylesheet" href="/member/css/common/hirakaku_1610261.css" type="text/css" /> ');} 15jQuery(function(){ 16var d = new Detector(); 17if (d.test('メイリオ')){$("head").append('<link rel="stylesheet" href="/member/css/common/meiryo_1610261.css" type="text/css" /> '); 18$.cookie("isBroadFont", '1', cookieOptions);} else if (d.test('HiraKakuPro-W3')){$("head").append('<link rel="stylesheet" href="/member/css/common/hirakaku_1610261.css" type="text/css" /> ');$.cookie("isBroadFont", '2', cookieOptions);}}); 19//]]> 20</script> 21<script type="text/javascript" charset="utf-8"> 22//<![CDATA[ var _gaq = _gaq || []; 23 _gaq.push(['_setAccount', 'UA-37292219-1']); 24 _gaq.push(['_setDomainName', 'www.watashi-move.jp']); 25 _gaq.push(['_trackPageview']); 26(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 27var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})(); 28//]]> 29</script></head> 30<body> 31<!-- header area start --> 32<div id="headerArea"><div id="headerTop"> 33<h1><a name="home" href="https://www.watashi-move.jp/wl/home/index.php"><img src="/wl/images/common/logo_wellnesslink_0001.gif" alt="WellnessLINK" width="190" height="24" /></a></h1> 34<ul><li class="noPipe"><a href="https://www.watashi-move.jp/member/profile/index.php" title="WM会員情報">WM会員情報</a></li><li ><a href="https://www.watashi-move.jp/member/support/index.php" title="サポート">サポート</a></li><li ><a href="http://www.watashi-move.jp/member/logout.php" title="ログアウト">ログアウト</a></li><li id="wmLogo" ><ahref="https://www.watashi-move.jp/member/home/index.php"><img src="/wl/images/common/logo_wm_0001.gif" alt="私ムーブ" width="58" height="24" /></a></li></ul></div> 35<div id="globalNav"> 36<p id="statusUser"><a href="https://www.watashi-move.jp/member/profile/index.php" name="globalNavi">*さん</a></p> 37<ul><li><a href="https://www.watashi-move.jp/wl/home/index.php" name="globalNavi"><img src="/wl/images/common/globalnav_home_0001.gif" alt="ホーム" width="110" height="40" class="rollover" /></a></li><li><a href="https://www.watashi-move.jp/wl/mygraph/index.php" name="globalNavi"><img src="/wl/images/common/globalnav_graph_0001.gif" alt="グラフ" width="130" height="40" class="rollover" /></a></li></ul></div></div> 38<!-- header area end --><!-- container area start --> 39<div id="contentArea"> 40<!-- main area start --> 41<div id="content"><div id="contentWide"> 42<form id="vitalForm" name="sphygmomanometerform" method="post" action=""> 43<div class="navi clear"> 44<h2><img src="/wl/images/common/icon_measurementdata_0001.gif" alt="" width="41" height="41" class="icon" /><img src="/wl/images/h2_datalist_0001.gif" alt="メモ" width="114" height="18" /></h2> 45<ul><li class="link"><div class="addLink"><p><img height="11" width="13" src="/wl/images/common/arrow2_0001.gif" alt=""> <a href="http://www.watashi-move.jp/pc/wm/help_m/index.html#58" class="subWindowOpen">ヘルプ</a></p></div></li></ul> 46<ul><li><a class="backButton" href="sphygmomanometer.php?targetDate=2016/06/01"><img src="/wl/images/common/btn_date_back_0001.gif" alt="戻る" width="81" height="34" class="rollover" /></a></li><li id="date1" class="date">2016/07</li><li><a class="nextButton" href="sphygmomanometer.php?targetDate=2016/08/01"><img src="/wl/images/common/btn_date_next_0001.gif" alt="進む" width="81" height="34" class="rollover" /></a></li><li class="space"> </li><li class="space cancel"> </li><li class="space save"><a id="toEditMode" class="buttonLink" href="sphygmomanometer.php?editMode=editMode&line=1&targetDate=2016/07/01"><img src="/wl/images/common/btn_edit_0002.gif" alt="編集" width="142" height="34" class="rollover" /></a></li></ul></div> 47<div class="notifyMessageArea clear"><div class="notifyMessage"><p> 48 </p></div> 49<div class="adv"><p> </p></div></div> 50<ul class="tab clear"><li class="stay"><strong>血圧計</strong></li> 51<li><a class="tabLink" href="memo.php?targetDate=2016/07/01">メモ<a></li></ul> 52<div class="box01Rounded clear"><div class="attention"><div class="hint"><span class="aten">※</span>入力必須</div> 53<div class="links"><a href="http://www.watashi-move.jp/wl/mydata/csv.php?c=2&targetDate=2016/07/01" style="margin-left:5px">出力</a> 54<a href="http://www.watashi-move.jp/wl/mydata/csv_all.php?c=2" onclick="return confirmSubmit();" style="margin-left:5px">出力</a> 55<a href="http://www.watashi-move.jp/wl/mydata/MyData_HEM_201607.pdf?time=1478700665" target="_blank" style="margin-left:5px">印刷</a></div></div> 56<div style="display:none;"><div style="display:none;" id="dummyArea"></div></div> 57<div class="header"><table><tr class="headerLine1"> 58<th colspan="4" rowspan="2" class="date">2016年7月</th> 59<th rowspan="2" class="time"><div class="glossary">時刻<span class="aten">※</span></th> 60<th colspan="3" class="bloodPressure">血圧<span class="aten">※</span><em> (mmHg)</em></th><th rowspan="2" class="pulse">脈拍<br /> 61<em> (拍/分)</em></th><th colspan="2" class="takingMedicine">服薬</th><th rowspan="2" class="bpmemo">手帳<em></em></th><th rowspan="2" class="delete">削除</th><th rowspan="2" class="forScrollBar"> </th></tr> 62<tr class="headerLine2"><th class="pressureMax">最高血圧</th><th class="pressureSplit">/</th><th class="pressureMin">最低血圧</th><th class="takeMedicine">飲んだ</th><th class="notTakeMedicine">飲まな<br />かった</th></tr></table></div> 63<div class="datas" > 64<table id="sphygmomanometertable" class="datalist"> 65<tr id="listTr13" class="day13"> 66<td class="day">13</td> 67<td class="week">水</td> 68<td class="memo"> </td> 69<td class="btnAdd"></td> 70<td class="time">13:44</td> 71<td class="pressureMax"><span>93</span></td> 72<td class="pressureSplit">/</td> 73<td class="pressureMin"><span>59</span></td> 74<td class="pulse_rate"><span>77</span></td> 75<td class="takeMedicine"><input type="radio" name="take_medicine[13]" id="radio13-1" value="1" readonly="readonly" disabled="disabled" /></td> 76<td class="notTakeMedicine"><input type="radio" name="take_medicine[13]" id="radio13-2" value="2" readonly="readonly" disabled="disabled" /></td> 77<td class="bpmemo"></td> 78<td class="btnDelete"> <input type="hidden" id="day13" name="day[13]" value="13" /><input type="hidden" class="auto_flg" id="auto_flg13" name="auto_flg[13]" value="1" /><input type="hidden" id="day_group13" name="day_group[13]" value="13" /><input type="hidden" name="measure_start[13]" value="" /></td></tr> 79</table></div> 80</body> 81</html>
HTMLと合ってないと言うことでしょう。HTMLを見ないことには何とも言い難いですね。
当該「>Webページはオムロンのウェルネスリンク」を開き、ブラウザ上の右クリックからソース表示したタグの構成が分かれば、doc.xpathの分解と一致しているか判別できます。
HTMLを記載したいのですが、文字数制限で10000字を超えていないのに規制がかかって投稿できないのでもう少し待っていただけますか?申し訳ありません。
練習として、もっと簡単なサイトでのスクレイピングから始めてはどうでしょうか。
学校の研究でいろいろやっていて、このサイトを使っていてのですが、初心者には向いてないですね。。。htmlの投稿ができたのでみていただけたら嬉しいです…13日以外のデータは構造は同じなので文字数の関係で省略しました。よろしくお願い致します。
回答1件
あなたの回答
tips
プレビュー