【发布时间】: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