YQL で html を読み込む方法が変わった(html table is no longer supported.)
id:algon-320 さんのウィジェットを使わせていただいて、
rating を取得していたのだけれど、ある日AtCoderのratingが
取得できなくなっていた。
返ってきたjsonを見てみると
html table is no longer supported. See https://policies.yahoo.com/us/en/yahoo/terms/product-atos/yql/index.htm for YQL Terms of Use
と出ていた。
その日ずっとドキュメントを見ていたが解決できず…。
今日もう一度調べてみたらStack Overflowが引っ掛かった。
htmlstring
というのを使うようになったらしい。
出力も色々変わったようなので新しい仕様に合わせて
rating取得部分を修正。
function getAtCoderRating() { var userpage = 'https://atcoder.jp/user/' + handles['atcoder']; var yql = "select * from htmlstring where url='" + userpage + "' and xpath='//*[@id=\\'main-div\\']/div/div/div[2]/dl/dd[2]/span'&format=json&env=store://datatables.org/alltableswithkeys&callback="; var url = 'https://query.yahooapis.com/v1/public/yql?q=' + encodeURI(yql); $.ajax({ type: 'GET', url: url, dataType: 'json', timeout: 10000, cache: false, success: function(json) { if(json['query']['results'] != null) { ratings['atcoder'] = Number($("span:eq(0)", "<div>" + JSON.stringify(json['query']['results']['result']) + "</div>").text()); setHtml('atcoder'); } else { setHtmlWithoutColor('atcoder'); } }, error: function() { setHtmlWithoutColor('atcoder'); } }); }
ちょっとjavascript慣れてなくてスマートな感じではないですが…。
これで色付けできるようになりました。
こういうの、ドキュメントのどこを調べると分かるのだろう…。
今回は「後でStack Overflowに出るでしょ」と思ったので放置したけど、
そうはいかない場面に出くわしたときに対処できるようになりたい。