JSoup 无法获得 Google 搜索结果的正确日期范围

JSoup won't get the right date range for Google Search results

我在这里潜伏了一段时间,但我今天正式加入,因为我在使用 JSoup 时遇到了一个大问题。我正在创建一个程序,我需要在其中收集 Google News 在您搜索特定主题时提供的小标题和描述。

当我使用此代码时:

(x.getURL() 在新闻中给出一个google搜索页面,从某个日期到另一个某个日期)

    URLBuilder x = new URLBuilder(q, from.getMmddyyyy(), to.getMmddyyyy());

    String p = x.getURL();
    System.out.println(p);
    doc = Jsoup.connect(p).userAgent("Mozilla").get();
    System.out.println(doc);

    Elements links = doc.select("h3[class=_hJs");
    for (Element link : links) {
        Elements titles = link.select("a[class=l _PMs]");
        String title = titles.text();

        Elements bodies = link.select("div[class=st]");
        String body = bodies.text();

        System.out.println("Title: "+title);
        System.out.println("Body: "+body+"\n");
    }

该代码运行良好,但唯一的问题是,如果您现在要 google 我的查询,它会给我 html 我想要的标题和正文在新闻中。例如,我的 URL 生成器会给出 URL(以及输出中打印的 URL):

https://google.com/search?q=Apple+&tbm=nws&num=100&tbs=cdr%3A1%2Ccd_min%3A01%2F19%2F2016%2Ccd_max%3A01%2F23%2F2016

如果您关注 google,您会得到 "Apple" 的搜索结果,每页 100 个结果,来自新闻,从 2016 年 1 月 19 日到 1/仅限 23/2016。 但是,当我在下一行中打印出 HTML 文档时,它会从 google 搜索结果中给出 HTML "Apple" 每页 100 个结果,来自新闻,但从当前日期开始。

这里发生了什么?我已经尝试了所有方法,但为什么 JSoup 不从该特定页面提供 HTML???我已经尝试了所有不同类型的 .userAgents,包括不完全使用它,但没有任何效果。

说真的,如果您甚至有 inkling 解决方案的想法,请与我分享。我会尝试任何事情,这件事一直在折磨我。

当您尝试连接 Jsoup 时,它会显示最新消息。我不知道为什么会这样,但是您可以在浏览器中访问具有日期范围的页面。因此,您可以使用 selenium 来抓取该页面,这是您可以做到的方法;

WebDriver driver = null;
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:\Users\PER\Desktop\phantomjs-2.1.1-windows\bin\chrome.exe");
    cap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    cap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "accept-encoding", "gzip, deflate, sdch, br");
    cap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", "en-US;q=0.6,en;q=0.4");
    cap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");

    driver = new ChromeDriver(cap);
    driver.navigate().to("https://google.com/search?q=Apple+&tbm=nws&num=100&tbs=cdr%3A1%2Ccd_min%3A01%2F19%2F2016%2Ccd_max%3A01%2F23%2F2016");;

    String doc = driver.getPageSource();
    Document document = Jsoup.parse(doc);
    System.out.println(doc);

    Elements links = document.select("h3[class=_hJs");
    for (Element link : links) {
        Elements titles = link.select("a[class=l _PMs]");
        String title = titles.text();

        Elements bodies = link.select("div[class=st]");
        String body = bodies.text();

        System.out.println("Title: "+title);
        System.out.println("Body: "+body+"\n");
    }

您还需要导入这些;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

此代码有效:

package jhy;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;

public class NewsTest {
    public static void main(String... args) throws IOException {
        String url = "https://google.com/search?q=Apple+&tbm=nws&num=100&tbs=cdr%3A1%2Ccd_min%3A01%2F19%2F2016%2Ccd_max%3A01%2F23%2F2016";
        Document doc = Jsoup.connect(url).get();

        Elements links = doc.select("h3 a");
        for (String title : links.eachText()) {
            System.out.println(title);
        }
    }
}

并输出:

Apple's First iOS App Development Center In Europe Will Be ...
Apple wins court order banning sale of old Samsung phones
Apple, Microsoft, Samsung Linked To Child Mining In DRC, Says ...
Apple Eyes Launching Stores In India
Apple Veteran Overseeing Electric-Car Project Leaving Company

请确保您使用的是 latest version of jsoup(今天是 1.10.3)。 URL 中的 %xx 重定向在先前版本中的处理方式存在错误,这可能会影响您。