没有解析谷歌新闻内容的输出

eva*_*abb 5 java parsing google-search google-search-api jsoup

对于我的代码,我想获得谷歌新的搜索标题和URL.

它过去有效.但是,我不知道为什么它现在不起作用?

谷歌改变了它的CSS结构还是什么?

谢谢

   public static void main(String[] args) throws UnsupportedEncodingException, IOException {

        String google = "http://www.google.com/search?q=";

        String search = "stackoverflow";

        String charset = "UTF-8";

        String news="&tbm=nws";


        String userAgent = "ExampleBot 1.0 (+http://example.com/bot)"; // Change this to your company's name and bot homepage!

        Elements links = Jsoup.connect(google + URLEncoder.encode(search , charset) + news).userAgent(userAgent).get().select( ".g>.r>.a");

        for (Element link : links) {
            String title = link.text();
            String url = link.absUrl("href"); // Google returns URLs in format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".
            url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");

            if (!url.startsWith("http")) {
                continue; // Ads/news/etc.
            }
            System.out.println("Title: " + title);
            System.out.println("URL: " + url);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Pro*_*ock 3

如果问题是“如何让代码再次运行?” 任何人都很难知道旧页面是什么样子,除非他们保存了副本。

我像这样分解了你的选择,它对我有用。

    String string = google + URLEncoder.encode(search , charset) + news;
    Document document = Jsoup.connect(string).userAgent(userAgent).get();
    Elements links = document.select( ".r>a");
Run Code Online (Sandbox Code Playgroud)

当前页面源看起来像

       <div class="g">
        <table>
         <tbody>
          <tr>
           <td valign="top" style="width:516px"><h3 class="r"><a href="/url?q=https://www.bleepingcomputer.com/news/security/marlboro-ransomware-defeated-in-one-day/&amp;sa=U&amp;ved=0ahUKEwis77iq7cDRAhXI7IMKHUAoDs0QqQIIFCgAMAE&amp;usg=AFQjCNFFx-sJdU814auBfquRYSsct2c8WA">Marlboro Ransomware Defeated in One Day</a></h3>
Run Code Online (Sandbox Code Playgroud)

结果:标题:万宝路勒索软件一天内被击败 URL: https: //www.bleepingcomputer.com/news/security/marlboro-ransomware-defeated-in-one-day/

标题:Stack Overflow 为开发人员的简历注入了新的活力 URL:https ://techcrunch.com/2016/10/11/stack-overflow-puts-a-new-spin-on-resumes-for-developers/

已编辑 - 时间范围 这些 URL 参数看起来很糟糕。
添加后缀 &tbs=cdr%3A1%2Ccd_min%3A5%2F30%2F2016%2Ccd_max%3A6%2F30%2F2016

但这部分“min%3A5%2F30%2F2016”包含您的最小日期。5 30 2016. min%3A + (一年中的月份) + %2F + (月份中的日期) + %2F + 年 而“max%3A6%2F30%2F2016”中是您的最大日期。6 30 2016。最大%3A +(一年中的月份)+ %2F +(一个月中的日期)+%2F + 年

以下是在 05/30/2016 至 06/30/2016 之间搜索 Mindy Kaling 的完整网址 https://www.google.com/search?tbm=nws&q=mindy%20kaling&tbs=cdr%3A1%2Ccd_min%3A5%2F30% 2F2016%2Ccd_max%3A6%2F30%2F2016