2014-11-25 39 views
1

我試圖連接到google的趨勢並解析他們的迴應。我有這樣的代碼。Java中的URCPonnection編碼

String urlstr = "http://www.google.com/trends/fetchComponent?q=seattle&cid=TIMESERIES_GRAPH_0&export=3"; 

    URL url; 

    String responsestr = null; 
    try { 
     url = new URL(urlstr); 
     //Reader reader = new InputStreamReader(url.openStream(),"UTF-8"); 
     URLConnection connection = url.openConnection(); 
     connection.setRequestProperty("Accept-Charset", "UTF-8"); 
     InputStream response = connection.getInputStream(); 
     Reader reader = new InputStreamReader(response); 
    }catch(Exception e){} 

可以確認網址是否正確,只需複製粘貼網址到瀏覽器,不過,雖然我的代碼通過,並且與谷歌的連接,這對谷歌的話說,一邊返回一個錯誤,它可以不處理我的請求。我在編碼或URL中做錯了什麼,或者我如何設置URLConnection。任何幫助,將不勝感激!

+0

你的代碼是我的系統上正常工作。你得到什麼異常? – 2014-11-25 04:58:35

+0

我剛剛發現谷歌放在查詢的數量上限,如果查詢超過閾值,阻止你的IP地址......我想這是沒有好的解決方案:( – wasabi 2014-11-25 05:14:44

+0

我可能嵌入了可嵌入版本的代碼到一個網站來解決這個問題? – wasabi 2014-11-25 05:15:29

回答

1

獲得如下響應。

public static void main(String[] args) { 
     String urlstr = "http://www.google.com/trends/fetchComponent?q=seattle&cid=TIMESERIES_GRAPH_0&export=3"; 
     URL url; 
     try { 
      url = new URL(urlstr); 
      // Reader reader = new InputStreamReader(url.openStream(),"UTF-8"); 
      URLConnection connection = url.openConnection(); 
      connection.setRequestProperty("Accept-Charset", "UTF-8"); 
      BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      String inputLine; 
      while ((inputLine = in.readLine()) != null) 
       System.out.println(inputLine); 
      in.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

輸出

<!DOCTYPE html><html ><head><meta http-equiv="X-UA-Compatible" content="IE=8,IE=9" /><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Google トレンド - エラー�検出�れ���</title><meta name="description" content="Google トレンド - エラー�検出�れ���" /><meta itemprop="name" content="Google トレンド - エラー�検出�れ���" /><meta itemprop="image" content="http://www.google.com/trends/resources/2327917647-google-icon.png" /><meta itemprop="description" content="Google トレンド - エラー�検出�れ���" /><meta property="og:title" content="Google トレンド - エラー�検出�れ���" /><meta property="og:type" content="article" /><meta property="og:url" content="http://www.google.com/trends/errorPage" /><meta property="og:image" content="http://www.google.com/trends/resources/2327917647-google-icon.png" /><meta property="og:description" content="Google トレンド - エラー�検出�れ���" /><style type="text/css">#gbar,#guser{font-size:13px;padding-top:0px !important;}#gbar{height:22px}#guser{padding-bottom:7px !important;text-align:right}.gbh,.gbd{border-top:1px solid #c9d7f1;font-size:1px}.gbh{height:0;position:absolute;top:24px;width:100%}@media all{.gb1{height:22px;margin-right:.5em;vertical-align:top}#gbar{float:left}}a.gb1,a.gb4{text-decoration:underline !important}a.gb1,a.gb4{color:#00c !important}.gbi .gb4{color:#dd8e27 !important}.gbf .gb4{color:#900 !important}</style><link href="/trends/resources/2521744678-explore-css.css" media="screen" rel="Stylesheet" type="text/css" /><link href="/trends/resources/578362384-trends_ja.css" media="screen" rel="Stylesheet" type="text/css" /><script type="text/javascript">var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-4401283-1']); _gaq.push(['_setDomainName', 'google.com']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_setCookiePath', '/trends']);_gaq.push(['_trackPageview']); 
    (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'; 
     var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })(); 
    </script><script src="https://www.google.com/jsapi" type="text/javascript"></script><script src="/trends/resources/28734845-alljs-bin__ja.js" type="text/javascript"></script><script type="text/javascript">var layout; 

     function init() { 
     layout = new trends.Layout('http://support.google.com/trends/?hl=ja', true); 

     }; 
     </script></head><body onload="init()" ng-app="trends" class="non-touch-display"><div class="layout-container bobby-one-bar"><div class="wrap"><div class="main"><div class="zeitgeist-banner-2013-max-mode-hide"><div id=gbar><nobr><a target=_blank class=gb1 href="https://www.google.co.jp/webhp">検索</a> <a target=_blank class=gb1 href="http://www.google.co.jp/imghp?hl=ja">ç」»åƒ?</a> <a target=_blank class=gb1 href="http://maps.google.co.jp/maps?hl=ja">地図</a> <a target=_blank class=gb1 href="https://play.google.com/?hl=ja">Play</a> <a target=_blank class=gb1 href="http://www.youtube.com/">YouTube</a> <a target=_blank class=gb1 href="http://news.google.co.jp/nwshp?hl=ja">ニュース</a> <a target=_blank class=gb1 href="https://mail.google.com/mail/">Gmail</a> <a target=_blank class=gb1 href="https://drive.google.com/">ドライブ</a> <a target=_blank class=gb1 style="text-decoration:none" href="http://www.google.co.jp/intl/ja/options/"><u>ã‚‚ã?£ã?¨è¦‹ã‚‹</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a target=_top id=gb_70 href="https://www.google.com/accounts/ServiceLogin?service=trendspro&passive=1209600&continue=http://www.google.com/trends/errorPage?q%3Dseattle%26cid%3DTIMESERIES_GRAPH_0%26export%3D3&followup=http://www.google.com/trends/errorPage?q%3Dseattle%26cid%3DTIMESERIES_GRAPH_0%26export%3D3" class=gb4>ログイン</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div><div style="display:none;"><div id="trends-search-bar-tooltip"><div class="trends-search-bar-tooltip-title">トレンドを調ã?¹ã‚‹</div><div class="trends-search-bar-tooltip-explanation">ä»»æ„?ã?®ãƒˆãƒ」ックã?®æ¤œç´¢ã‚¤ãƒ³ã‚¿ãƒ¬ã‚¹ãƒˆã‚’調ã?¹ã‚‹ã?「ã?¨ã?Œã?§ã??ã?¾ã?™ã€‚</div><div class="trends-search-bar-tooltip-examples"><a href="/trends/explore#q=%2Fm%2F05p0rrx"><div class="trends-search-bar-tooltip-example">Bitcoin</div></a><a href="/trends/explore#q=%2Fm%2F0524b41"><div class="trends-search-bar-tooltip-example">Game of Thrones</div></a><a href="/trends/explore#q=%2Fm%2F01jz6d"><div class="trends-search-bar-tooltip-example">LeBron James</div></a><a href="/trends/explore#q=%2Fm%2F02mjmr"><div class="trends-search-bar-tooltip-example">Barack Obama</div></a></div></div></div><script type="text/javascript">var searchBar = new trends.SearchBar(true);</script><div ng-controller="SearchBarCtrl" ng-init="explorePagePath='/trends/explore'; init(false)" class="searchbar-dropdown"><autocomplete-dropdown edit="edit" query="query" selected-term="selectedTerm" register-key-handler="registerKeyHandler(fn)" num-edits-done="numEditsDone" min-input-length="2" num-results="6" enable-entities=true></autocomplete-dropdown></div></div><div class="app-bar zeitgeist-banner-2013-max-mode-hide" id='sticky-app-bar'><div class="butterBar-container" style="display: none;"><div id='butterBarWrap' class='butterBar-wrap'></div></div><span class="app-bar-title"><span class="jfk-productName">トレンド</span><div class="app-bar-container"></div></div><div class="sidebar-layout clear zeitgeist-banner-2013-max-mode-hide"><div class="sidebar"><div id="trends-sidebar-nav" class="sidebar-nav"><ul><li id="sidebar-hottrends" class="sidebar-nav-item "><a href="/trends/hottrends">急上昇ワード検索</a></li><li id="sidebar-hotvideos" class="sidebar-nav-item "><a href="/trends/hotvideos">Youtube ã?§ã?®æ³¨ç›®å‹•ç」»</a></li><div id="topcharts-nav-item" style="display: none;" ><li id="sidebar-topcharts" class="sidebar-nav-item "><a href="/trends/topcharts">ランキング</a></li></div><div id="topcharts-zippy-nav-item"><div id="sidebar-topcharts-zippy-header"><li id="sidebar-topcharts" class="sidebar-nav-item "><a href="/trends/topcharts">ランキング</a></li></div><div id="sidebar-topcharts-zippy-content" class="jfk-zippy-content"><div class="sidebar-filters"><a href='/trends/topcharts#vm=cat&cid=' class='topcharts-category-menu-link' id='' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', '']);"><div class="sidebar-filter" id="cat-"><div>ã?™ã?¹ã?¦</div></div></a><a href='/trends/topcharts#vm=cat&cid=entertainment' class='topcharts-category-menu-link' id='entertainment' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'entertainment']);"><div class="sidebar-filter" id="cat-entertainment"><div>エンターテインメント</div></div></a><a href='/trends/topcharts#vm=cat&cid=shopping' class='topcharts-category-menu-link' id='shopping' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'shopping']);"><div class="sidebar-filter" id="cat-shopping"><div>ショッãƒ」ング&ファッション</div></div></a><a href='/trends/topcharts#vm=cat&cid=sports' class='topcharts-category-menu-link' id='sports' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'sports']);"><div class="sidebar-filter" id="cat-sports"><div>スãƒ?ーツ</div></div></a><a href='/trends/topcharts#vm=cat&cid=business_and_politics' class='topcharts-category-menu-link' id='business_and_politics' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'business_and_politics']);"><div class="sidebar-filter" id="cat-business_and_politics"><div>ãƒ「ジãƒ?スã€?æ」¿æ²»</div></div></a><a href='/trends/topcharts#vm=cat&cid=lifestyle' class='topcharts-category-menu-link' id='lifestyle' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'lifestyle']);"><div class="sidebar-filter" id="cat-lifestyle"><div>ライフスタイル</div></div></a><a href='/trends/topcharts#vm=cat&cid=nature_and_science' class='topcharts-category-menu-link' id='nature_and_science' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'nature_and_science']);"><div class="sidebar-filter" id="cat-nature_and_science"><div>自然科学</div></div></a><a href='/trends/topcharts#vm=cat&cid=cities' class='topcharts-category-menu-link' id='cities' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'cities']);"><div class="sidebar-filter" id="cat-cities"><div>都é?「府県</div></div></a><a href='/trends/topcharts#vm=cat&cid=travel_and_leisure' class='topcharts-category-menu-link' id='travel_and_leisure' onclick="_gaq.push(['_trackEvent', 'click', 'sidebar-category', 'travel_and_leisure']);"><div class="sidebar-filter" id="cat-travel_and_leisure"><div>æ—…è¡Œã€?レジャー</div></div></a></div></div><script type="text/javascript">trends.Utils.createZippy('sidebar-topcharts-zippy-header', 'sidebar-topcharts-zippy-content',false);</script></div><li id="sidebar-trends" class="sidebar-nav-item "><a href="/trends/explore">調ã?¹ã‚‹</a></li><li id="sidebar-trends" class="sidebar-nav-item "><a href="/trends/subscriptions">登録</a></li></ul></div></div><div class="center-col"><div id="reportContent" class="report-content"><div id="report"><div class="errorTitle">エラーã?®ç™ºç」Ÿ</div><div class="errorSubTitle">割りå½「ã?¦ã?®ä¸Šé™?ã?«é?」ã?—ã?¦ã?„ã?¾ã?™ã€‚ã?—ã?°ã‚‰ã??ã?—ã?¦ã?‹ã‚‰ã‚‚ã?†ä¸€åº¦ã?Šè©¦ã?—ã??ã? ã?•ã?„。</div></div></div></div></div></div></div><div class="clear"></div><div class="footer zeitgeist-banner-2013-max-mode-hide"><div><div class="footer-item"><a href="/intl/ja/about/">Google ã?«ã?¤ã?„ã?¦</a></div><div class="footer-item"><a href="/intl/ja/policies/">プライãƒ?シー ãƒ?リシーã?¨åˆ©ç」¨è¦?ç´„</a></div><div class="footer-item"><a href="http://support.google.com/trends/?hl=ja">ヘルプセンター</a></div><div class="footer-item send-feedback" onclick="trends.feedback.startFeedback('ja')"><a>フィードãƒ?ックをé€?ä¿¡</a></div><div class="footer-more">ã??ã?®ä»–:<a href="//www.google.com/trends/correlate">Google Correlate</a><a href="//www.google.org/flutrends">Google Flu Trends</a><a href="//www.youtube.com/trendsdashboard">YouTube Trends</a></div></div></div></div><script></script></body></html>