【问题标题】:Web scraping with jsoup and selenium使用 jsoup 和 selenium 进行网页抓取
【发布时间】:2015-02-27 12:38:07
【问题描述】:

我想用 selenium 和 jsoup 从这个动态网站中提取一些信息。要获取我想要提取的信息,我必须单击“详细信息 öffnen”按钮。第一张图片显示的是单击按钮之前的网站,第二张图片显示的是单击按钮后的网站。红色标记的信息是我要提取的信息。

我最初尝试仅使用 Jsoup 提取信息,但有人告诉我 Jsoup 无法处理动态内容,所以我现在尝试使用 selenium 和 Jsoup 提取信息,就像您在源代码中看到的那样。但是,我不确定 selenium 是否适合于此,所以也许还有其他方法可以更简单地提取我需要的信息,但重要的是可以使用 Java 来完成。

接下来的两张图片显示了点击按钮之前和点击之后的html代码。

public static void main(String[] args) {
    
    WebDriver driver = new FirefoxDriver(createFirefoxProfile());
    driver.get("http://www.seminarbewertung.de/seminar-bewertungen?id=3448");
    //driver.findElement(By.cssSelector("input[type='button'][value='Details öffnen']")).click();
    WebElement webElement = driver.findElement(By.cssSelector("input[type='submit'][value='Details öffnen'][rating_id='2318']"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", webElement);
    String html_content = driver.getPageSource();
    //driver.close();
    
    
    Document doc1 = Jsoup.parse(html_content);
    System.out.println("Hallo");
    
    Elements elements = doc1.getAllElements();
    for (Element element : elements) {
        System.out.println(element);
    }

}

private static FirefoxProfile createFirefoxProfile() {
    File profileDir = new File("/tmp/firefox-profile-dir");
    if (profileDir.exists())
        return new FirefoxProfile(profileDir);
    FirefoxProfile firefoxProfile = new FirefoxProfile();
    File dir = firefoxProfile.layoutOnDisk();
    try {
        profileDir.mkdirs();
        FileUtils.copyDirectory(dir, profileDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return firefoxProfile;
}

使用此源代码,我无法找到包含我要提取的信息的 div 元素。

如果有人能帮我解决这个问题,那就太好了。

【问题讨论】:

  • 您可能想尝试 Selenium IDE。在那里,您可以记录操作,然后将其翻译成 Selenese - Selenium 语言。从中您可以将代码迁移到 Java。

标签: java html selenium web-scraping jsoup


【解决方案1】:

确实,如果 Jsoup 是 javascript 生成的,则它无法处理动态内容,但在您的情况下,按钮正在发出 Ajax 请求,这可以通过 Jsoup 很好地完成。

我建议进行调用以检索按钮及其 ID,然后进行连续调用(Ajax 帖子)以检索详细信息(cmets 或其他)。

代码可能是:

    Document document = Jsoup.connect("http://www.seminarbewertung.de/seminar-bewertungen?id=3448").get();
    //we retrieve the buttons
    Elements select = document.select("input.rating_expand");
    //we go for the first
    Element element = select.get(0);
    //we pick the id
    String ratingId = element.attr("rating_id");

    //the Ajax call
    Document document2 = Jsoup.connect("http://www.seminarbewertung.de/bewertungs-details-abfragen")
            .header("Accept", "*/*")
            .header("X-Requested-With", "XMLHttpRequest")
            .data("rating_id", ratingId)
            .post();

    //we find the comment, and we are done
    //note that this selector is only as a demo, feel free to adjust to your needs
    Elements select2 = document2.select("div.ratingbox div.panel-body.text-center");
    //We are done!
    System.out.println(select2.text());

此代码将打印所需的:

Das Eingehen auf individuelle Bedürfnisse eines jeden einzelnen Teilnehmer scheint mir ein Markenzeichen von Fromm zu sein。 Bei einem früheren Seminar habe ich dies auch schon so erlebt!

我希望它会有所帮助。

新年快乐!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-21
    • 2019-02-12
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-06
    相关资源
    最近更新 更多