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();
Jsoup爬虫任务总结的更多相关文章
- jsoup爬虫简书首页数据做个小Demo
代码地址如下:http://www.demodashi.com/demo/11643.html 昨天LZ去面试,遇到一个大牛,被血虐一番,发现自己基础还是很薄弱,对java一些原理掌握的还是不够稳固, ...
- (java)Jsoup爬虫学习--获取智联招聘(老网站)的全国java职位信息,爬取10页
Jsoup爬虫学习--获取智联招聘(老网站)的全国java职位信息,爬取10页,输出 职位名称*****公司名称*****职位月薪*****工作地点*****发布日期 import java.io.I ...
- (java)Jsoup爬虫学习--获取网页所有的图片,链接和其他信息,并检查url和文本信息
Jsoup爬虫学习--获取网页所有的图片,链接和其他信息,并检查url和文本信息 此例将页面图片和url全部输出,重点不太明确,可根据自己的需要输出和截取: import org.jsoup.Jsou ...
- 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo
简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ...
- JSOUP爬虫示例
利用JSOUP做爬虫,爬取我博客中的所有标题加链接,代码示例如下: package com.test.jsoup; import java.io.IOException; import org.jso ...
- HttpClient&Jsoup爬虫的简单应用
详细的介绍已经有很多前辈总结,引用一下该篇文章:https://blog.csdn.net/zhuwukai/article/details/78644484 下面是一个代码的示例: package ...
- 利用jsoup爬虫工具,爬取数据,并利用excel导出
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...
- Jsoup爬虫解析
需要下载jsoup-1.8.1.jar包 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQue ...
- jsoup爬虫,项目实战,欢迎收看
import com.mongodb.BasicDBObject import com.mongodb.DBCollection import org.jsoup.Jsoup import org.j ...
随机推荐
- C++中逻辑操作符的重载分析
1,逻辑运算符的原生语义: 1,操作数只有两种值(true 和 false): 1,C 语言中只有非 0 和 0: 2,逻辑表达式不用完全计算就能确定最终值: 1,短路规则: 3,最终结果只能是 tr ...
- Vue中子组件数据跟着父组件改变和父组件数据跟着子组件改变的方法
一,子组件数据跟着父组件改变 父组件的代码 <template> <div class="home"> <img alt="Vue logo ...
- 关于GeneXus封装方法Model的方法
最近 刚从外地出差回来 工作任务不是很重 能够抽点时间记点东西 下午花了2个多钟头尝试了一下GeneXus的封装方法的功能,这里记一下便于自己以后查看.我们在许多项目中或多或少都会有着重复代码编写的 ...
- 四、bootstrap-Table
一.bootstrap-Table基础表格 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- sqlldr - exit error code 2 in unix (merged)
http://www.orafaq.com/forum/t/146582/ Thank you for your reply. Load has been successful all the tim ...
- 深度学习练手项目——DNN识别手写数字
该案例主要目的是为了熟悉Keras基本用法,以及了解DNN基本流程. 示例代码: import numpy as np import matplotlib.pyplot as plt from ker ...
- Vue路由组件vue-router
一.路由介绍 Creating a Single-page Application with Vue + Vue Router is dead simple. With Vue.js, we are ...
- C语言结构体实例-创建兔子
参考裸编程思想. #include <stdio.h> //#include "ycjobject.h" // 颜色定义 #define CL_BLACK 0 #def ...
- firewall&iptables小记
一.firewall 查看firewall状态 firewall-cmd --state 防火墙开启: 防火墙关闭: 如果firewall为关闭状态,先启动firewall systemctl sta ...
- 使用nexus3.10搭建maven私有仓库
使用nexus3.10搭建maven私有仓库-----详见如下链接-- --此贴用于笔记 https://blog.csdn.net/vipbupafeng/article/details/80232 ...