我想凑这个网站
https://www.dailystrength.org/search?query=aspirin&type=discussion
以获得一个数据集的一个项目,我有(使用阿司匹林作为占位符的搜索项)。
我决定用Jsoup使履带。但问题是,岗位动态与Ajax请求带来的。该请求是使用显示更多的按钮做
This button causes the problems
当显示的全部内容就应该是这样的文本“所有邮件加载”
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.*;
/**
*
* @author Ahmed
*/
public class Crawler {
public static void main(String args[]) {
Document search_result;
String requested[] = new String[]{"aspirin"/*, "Fentanyl"*/};
ArrayList<Newsfeed_item> threads = new ArrayList();
String query = "https://www.dailystrength.org/search?query=";
try {
for (int i = 0; i < requested.length; i++) {
search_result = Jsoup.connect(query+requested[i]+"&type=discussion").get();
Elements posts = search_result.getElementsByClass("newsfeed__item");
for (Element item : posts) {
Elements link=item.getElementsByClass("newsfeed__btn-container posts__discuss-btn");
Newsfeed_item currentItem=new Newsfeed_item();
currentItem.replysLink=link.attr("abs:href");
Document reply_result=Jsoup.connect(currentItem.replysLink).get();
Elements description = reply_result.getElementsByClass("posts__content");
currentItem.description=description.text();
currentItem.subject=requested[i];
System.out.println(currentItem);
}
}
} catch (IOException ex) {
Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
此代码给我,仅示出了几个帖子,而不是隐藏的帖子。我明白,JSoup不能用于这个问题,所以我试图找到硒可显示完整的内容,并下载它爬行的来源。
我找不到任何来源,唯一的代码发现,试图从初步认识
https://www.youtube.com/watch?v=g1IbI_qYsDg
给我这个错误
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:44)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:167)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:190)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
at SeleniumTest.main(SeleniumTest.java:14)
C:\Users\Ahmed\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
任何帮助或示例代码或替代方案?我只需要得到完整的页面,我用履带我。或者让一个全新的履带但比我找不到代码,我遇到错误。
我会尝试继续而不硒的方法。使用Web浏览器的调试器和网络选项卡,你可以在所有浏览器发送请求偷看。
这是非常有用看一看当你点击“显示更多”会发生什么。你可以看到有下一页从这个网址下载:https://www.dailystrength.org/search/ajax?query=aspirin&type=discussion&page=2&_=1549130275261你可以通过改变参数page=2
获得更多的网页。不幸的是,结果当属JSON逃脱含有HTML,所以你不得不使用一些JSON库解析它,获得HTML,然后用Jsoup解析它。这将是不错的,因为这JSON还包括可变"has_more":true
所以你知道,如果有更多的内容。