yahooのホットワードをRSSで取得することができる。
http://blog-search.yahoo.co.jp/buzzwords/rss
ここにHTTPリクエストを送信と、いま急上昇中のワードがXMLで返ってくる。
そのXMLを取得し、パースして標準出力に表示するサンプル。
package xml; import java.io.ByteArrayInputStream; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class GetYahooHotKeyWord { public static void main(String[] args) throws Exception { String xml = null; xml = getHotWordXml(); parse(xml); } private static String getHotWordXml() throws ClientProtocolException, IOException { String url = "http://blog-search.yahoo.co.jp/buzzwords/rss"; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity); } else { return null; } } private static void parse(String xml) throws Exception { DOMParser parser = new DOMParser(); try { parser.parse(new InputSource(new ByteArrayInputStream(xml.getBytes()))); Document doc = parser.getDocument(); NodeList items = doc.getElementsByTagName("channel"); for (int i = 0; i < items.getLength(); i++) { String word = null; String description = null; Element item = (Element) items.item(i); NodeList titleList = item.getElementsByTagName("title"); NodeList descList = item.getElementsByTagName("description"); for(int j = 0; j < titleList.getLength();j++) { if(titleList.item(j) != null) { System.out.println("急上昇キーワードは:" + titleList.item(j).getTextContent()); } if (descList.item(j) != null) { System.out.println("ポイントは:" + descList.item(j).getTextContent()); } } } } catch (Exception e) { e.printStackTrace(); } } }
結果はこんな感じになる。
急上昇キーワードは:風疹 ポイントは:急上昇ポイント: 700 急上昇キーワードは:その冬、風が吹く ポイントは:急上昇ポイント: 500
このキーワードについてのツイートをツイッターで取得するサンプルを後で作りたいが、ちょっとアメブロの自動投稿Javaの作成に苦戦中のため、まだ取り掛かれない。