1、相关jar包
jsoup-1.11.2.jar
log4j-1.2.8.jar
selenium-server-standalone-2.40.0.jar
可以自行找其它版本的jar包代替。
2、下载chromeDriver,并完成相关配置
注意:chromeDriver与chrome版本有关,如果版本不一致会发生错误。
下载chromeDriver:http://chromedriver.storage.googleapis.com/index.html
下载时一定要看清版本
chromeDriver下载完成之后,将chromeDriver.exe 放在chrome浏览器根目录下,或者在配置环境变量,为了方便我直接把chromeDriver.exe 放在了浏览器根目录下。
3、程序,以解析简书为例
3.1、初始化一个webDriver,并通过传入的网址获取到该网址的源码(打开浏览器,不能向下滑动)
/**
* 打开浏览器,不能向下滑动
* @param url
* @return
*/
public static Document getDocument(String url){
Document doc = null;
//可使用的浏览器有:IE浏览器(webdriver.ie.driver)
//火狐浏览器 (webdriver.gecko.driver)
//谷歌浏览器 (webdriver.chrome.driver)
// 是使用那个浏览器 chromedriver所在的位置
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
// InternetExplorerDriver() 浏览器
// FirefoxDriver() 火狐浏览器
//谷歌浏览器
WebDriver driver = new ChromeDriver();
driver.get(url);
//等待几秒
try {
//((JavascriptExecutor)driver).executeScript("scrollTo(0,10000)");
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
doc = Jsoup.parse(driver.getPageSource());
//关闭浏览器
driver.close();
driver.quit();
return doc;
}
3.2、解析获取到的内容
/**
* 解析传过来的doc
* @param doc
*/
public static void parse(Document doc){
if(doc == null){
logger.info("doc is null, unable to continue! ");
return ;
}
Elements content = doc.select("div.content");
//System.out.println(select);
for (Element element : content) {
//获取文章标题
String title = element.select("a.title").text();
//获取获取帖子网址
String url = element.select("a.title").attr("href");
url = "https://www.jianshu.com" + url;
//获取文章的摘要
String digest = element.select("p.abstract").text();
//获取文章作者名称
String author = element.select("a.nickname").text();
//获取作者网址
String authorUrl = element.select("a.nickname").attr("href");
authorUrl = "https://www.jianshu.com" + authorUrl;
logger.info("title: " + title);
logger.info("url: " + url);
logger.info("digest: " + digest);
logger.info("author: " + author);
logger.info("authorUrl: " + authorUrl);
logger.info("--------------\n");
}
}
3.3、查看测试结果
result.png
3.4、打开浏览器,滚动条向下滑动
/**
* 打开浏览器,向下滑动
* @param url
* @return
*/
public static Document getDocument(String url){
Document doc = null;
//可使用的浏览器有:IE浏览器(webdriver.ie.driver)
//火狐浏览器 (webdriver.gecko.driver)
//谷歌浏览器 (webdriver.chrome.driver)
// 是使用那个浏览器 chromedriver所在的位置
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
// InternetExplorerDriver() 浏览器
// FirefoxDriver() 火狐浏览器
//谷歌浏览器
WebDriver driver = new ChromeDriver();
driver.get(url);
//等待几秒
try {
//向下滚动 方法一
//JavascriptExecutor js = (JavascriptExecutor)driver;
//js.executeScript("scrollTo(0,20000)");
//向下滚动 方法二
((JavascriptExecutor)driver).executeScript("scrollTo(0,10000)");
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
doc = Jsoup.parse(driver.getPageSource());
//关闭浏览器
driver.close();
driver.quit();
return doc;
}
3.5、不打开浏览器
/**
* 浏览器后台运行,向下滑动
* @param url
* @return
*/
public static Document getDocument(String url){
Document doc = null;
//可使用的浏览器有:IE浏览器(webdriver.ie.driver)
//火狐浏览器 (webdriver.gecko.driver)
//谷歌浏览器 (webdriver.chrome.driver)
// 是使用那个浏览器 chromedriver所在的位置
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
// InternetExplorerDriver() 浏览器
// FirefoxDriver() 火狐浏览器
//谷歌浏览器
WebDriver driver = null;
//创建chrome参数对象
ChromeOptions options = new ChromeOptions();
//浏览器后台运行
options.addArguments("headless");
driver = new ChromeDriver(options);
driver.get(url);
//等待几秒
try {
//向下滚动 方法一
//JavascriptExecutor js = (JavascriptExecutor)driver;
//js.executeScript("scrollTo(0,20000)");
//向下滚动 方法二
((JavascriptExecutor)driver).executeScript("scrollTo(0,10000)");
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
doc = Jsoup.parse(driver.getPageSource());
//关闭浏览器
driver.close();
driver.quit();
return doc;
}
网友评论