1

我正在尝试连接到谷歌趋势并解析他们的响应。我有这样的代码。

    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 复制粘贴到浏览器中来检查 URL 是否正确,但是,尽管我的代码通过并与谷歌连接,但它在谷歌方面返回一个错误,说它无法处理我的请求。我在编码或 URL 或如何设置我的 URLConnection 中做错了。任何帮助,将不胜感激!

4

1 回答 1

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">地図&lt;/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">ニュース&lt;/a> <a target=_blank class=gb1 href="https://mail.google.com/mail/">Gmail</a> <a target=_blank class=gb1 href="https://drive.google.com/">ドライブ&lt;/a> <a target=_blank class=gb1 style="text-decoration:none" href="http://www.google.co.jp/intl/ja/options/"><u>ã‚‚ã?£ã?¨è¦‹ã‚‹&lt;/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>ログイン&lt;/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">トレンドを調ã?¹ã‚‹&lt;/div><div class="trends-search-bar-tooltip-explanation">ä»»æ„?ã?®ãƒˆãƒ”ックã?®æ¤œç´¢ã‚¤ãƒ³ã‚¿ãƒ¬ã‚¹ãƒˆã‚’調ã?¹ã‚‹ã?“ã?¨ã?Œã?§ã??ã?¾ã?™ã€‚&lt;/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">トレンド&lt;/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">急上昇ワード検索&lt;/a></li><li id="sidebar-hotvideos" class="sidebar-nav-item "><a href="/trends/hotvideos">Youtube ã?§ã?®æ³¨ç›®å‹•ç”»&lt;/a></li><div id="topcharts-nav-item"  style="display: none;" ><li id="sidebar-topcharts" class="sidebar-nav-item "><a href="/trends/topcharts">ランキング&lt;/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">ランキング&lt;/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>エンターテインメント&lt;/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>ショッピング&ファッション&lt;/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>スãƒ?ーツ&lt;/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>ビジãƒ?スã€?政治&lt;/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>ライフスタイル&lt;/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>自然科学&lt;/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>都é?“府県&lt;/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>æ—…è¡Œã€?レジャー&lt;/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">調ã?¹ã‚‹&lt;/a></li><li id="sidebar-trends" class="sidebar-nav-item "><a href="/trends/subscriptions">登録&lt;/a></li></ul></div></div><div class="center-col"><div id="reportContent" class="report-content"><div id="report"><div class="errorTitle">エラーã?®ç™ºç”Ÿ&lt;/div><div class="errorSubTitle">割り当ã?¦ã?®ä¸Šé™?ã?«é?”ã?—ã?¦ã?„ã?¾ã?™ã€‚ã?—ã?°ã‚‰ã??ã?—ã?¦ã?‹ã‚‰ã‚‚ã?†ä¸€åº¦ã?Šè©¦ã?—ã??ã? ã?•ã?„。&lt;/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/">プライãƒ?シー ãƒ?リシーã?¨åˆ©ç”¨è¦?ç´„&lt;/a></div><div class="footer-item"><a href="http://support.google.com/trends/?hl=ja">ヘルプセンター&lt;/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>
于 2014-11-25T04:58:19.643 回答