Jsoup爬虫任务总结

这两周由于公司需要大量数据爬取进数据库给用户展示素材,在不停的做爬虫工作,现在总算基本完成就剩清理数据的工作;

公司有一个采集器管理后台的项目,可以直接把爬虫代码打包成jar导入进去设置定时参数即可;

关于Jsoup的一些命令使用示例:

 解析html文档:

Document doc = Jsoup.parse(html);

从一个URL加载一个Document:
Document doc = Jsoup.connect("url").get();

示例一个通常的爬虫代码 :

public void testAddSBKK88EpubData() {
String url = "http://www.sbkk88.com/lizhishu/5fenzhonghemoshengrenchengweipengyou/";
String originUrl = "http://www.sbkk88.com";
FirefoxProfile firefoxProfile = new FirefoxProfile();
// 去掉css
//firefoxProfile.setPreference("permissions.default.stylesheet", 2);
// 去掉图片
//firefoxProfile.setPreference("permissions.default.image", 2);
// 去掉flash
firefoxProfile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", false);

FirefoxDriver driver = null;
try {
driver = new FirefoxDriver(firefoxProfile);
driver.get(url);
TimeUnit.SECONDS.sleep(3);
String html = driver.getPageSource();
//System.out.println(html);
org.jsoup.nodes.Document doc = Jsoup.parse(html);
Elements eles = doc.select("div.mingzhuLeft > ul.leftList li");
for(Element e:eles){

String title = e.select("a").text();
System.out.println(title);

String linkUrl = originUrl + e.select("a").attr("href");
org.jsoup.nodes.Document contentHtml = Jsoup.connect(linkUrl).get();
String content = contentHtml.select("div.f_article p").html();

}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (driver != null) {
driver.quit();
}
}
}


示例一个模拟登录网站代码:

private void login(String username, String password) {
driver.get(this.getRootUrl());
WebElement login = driver.findElement(By.xpath("//a[@class='in login fl']"));
if (login == null) {
return;
}
login.click();
WebElement phoneLogin = driver.findElement(By.xpath("//a[@class='do-phone-login']"));
while(phoneLogin == null){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
phoneLogin.click();
WebElement lastLogin = driver.findElement(By.xpath("//a[@class='sure-btn sure-success phone-login-btn']"));
while(lastLogin == null){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
driver.findElement(By.xpath("//input[@name='phone' and @class='form-put']")).clear();
driver.findElement(By.xpath("//input[@name='phone' and @class='form-put']")).sendKeys(username);
driver.findElement(By.xpath("//input[@name='passwd' and @class='form-put']")).clear();
driver.findElement(By.xpath("//input[@name='passwd' and @class='form-put']")).sendKeys(password);
lastLogin.click();
LOG.info("点击登录账号中~~~~~~~~~");
getPage();
// return focus to main window
// driver.switchTo().defaultContent();
}

还有模拟点击下载图片(可设置浏览器直接下载并自定义下载地址):

FirefoxProfile firefoxProfile = new FirefoxProfile();
// 去掉css
firefoxProfile.setPreference("permissions.default.stylesheet", 2);
// 去掉图片
firefoxProfile.setPreference("permissions.default.image", 2);
// 去掉flash
firefoxProfile.setPreference("dom.ipc.plugins.enabled.libflashplayer.so", false);
//设置默认下载
// 设置是否显示下载进度框
firefoxProfile.setPreference("browser.download.manager.showWhenStarting", false);
// browser.download.folderList 设置Firefox的默认 下载 文件夹。0是桌面;1是“我的下载”;2是自定义
firefoxProfile.setPreference("browser.download.folderList", 2);
// ,如果使用自定义路径,必须要将browser.download.folderList设置为2(下载到tomcat临时文件中)
firefoxProfile.setPreference("browser.download.dir", System.getProperty("java.io.tmpdir")+"\material_images");
// 设置哪种类型的文件下载不询问直接下载
firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","image/gif,image/png,image/jpeg,image/bmp,image/webp");

this.driver = new FirefoxDriver(firefoxProfile);

(采取下一个图片上传到阿里云,再删除原图片的措施)

//模拟点击免费下载
driver.findElement(By.id("detail_free_download_btn")).click();
File file = new File(System.getProperty("java.io.tmpdir")+"\material_images");
File[] files = file.listFiles();
//只允许有一个文件
if (files.length == 1) {
file = files[0];
} else {
for (File f : files) {
f.delete();
}
return;
}
//获取图片io流并上传到阿里云,同时将本地文件删除
String content = FileUtil.upload(new FileInputStream(file), file.getName());
LOG.info("图片已上传到阿里云:{}", content);
file.delete();

 
 
 
原文地址:https://www.cnblogs.com/yzf666/p/6909841.html