繁体   English   中英

使用 jsoup 和 selenium 进行网页抓取

[英]Web scraping with jsoup and selenium

我想用 selenium 和 jsoup 从这个动态网站中提取一些信息。 要获取我想要提取的信息,我必须单击“Details ö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 元素。

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

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

我建议拨打电话以检索按钮及其 ID,然后连续调用(Ajax 帖子)以检索详细信息(评论或其他内容)。

代码可能是:

    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!

我希望它会有所帮助。

祝新年快乐!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM