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 ...
随机推荐
- Ubuntu解压缩rar格式文件
解压缩rar文件时,出现问题 解决方法: sudo apt-get install unrar
- python学习第二天--字符串及格式化输出
# 字符串# 字符串取值:字符串名[索引值] 只能取单个值# 正序访问,从0开始str1 = "hello world"print(str1[3]) # 输出"l&quo ...
- smb.conf免密登录文件
# This is the main Samba configuration file. You should read the# smb.conf(5) manual page in order t ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(三)
未经书面许可.请勿转载 一张图简单概括 Simple Playbooks Ansible is useful as a command-line tool for making small chang ...
- fusionCharts图表在客户端导出图片
前提:要具备三个文件:FusionCharts.js / FusionChartsExportComponent.js / FCExporter.swf 1.引用 js 文件 <script t ...
- Windows开发,关于通过写代码加载PDB的那些事
最近,接到一个活,要写一个程序,用来批量分析一堆dll和对应的PDB, 其实工作很简单,就是根据一堆偏移,通过PDB文件,找到对应dll里面对应位置的明文符号, 简单的需求,实现起来,通常都很麻烦, ...
- 解决 Failed to load class "org.slf4j.impl.StaticLoggerBinder"
我们在使用日志记录网站或者应用时,有时候启动会出现这个告警: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder&q ...
- NOIP2016D1T3 换教室 (概率DP)
NOIP2016D1T3 换教室 题目大意:有n个时间段,每个时间段i有两个教室a[i],b[i]可以上课,如果不申请换教室就在教室a[i]上课,如果换教室就在b[i]上课.你最多只能换m次教室.教室 ...
- zxing opencv
- 解决error: Microsoft Visual C++ 14.0 is required 问题
1.https://964279924.ctfile.com/fs/1445568-239446865 2.重新安装 .Net framework 更高的版本:https://support.micr ...