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

公司有一个采集器管理后台的项目,可以直接把爬虫代码打包成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爬虫任务总结的更多相关文章

  1. jsoup爬虫简书首页数据做个小Demo

    代码地址如下:http://www.demodashi.com/demo/11643.html 昨天LZ去面试,遇到一个大牛,被血虐一番,发现自己基础还是很薄弱,对java一些原理掌握的还是不够稳固, ...

  2. (java)Jsoup爬虫学习--获取智联招聘(老网站)的全国java职位信息,爬取10页

    Jsoup爬虫学习--获取智联招聘(老网站)的全国java职位信息,爬取10页,输出 职位名称*****公司名称*****职位月薪*****工作地点*****发布日期 import java.io.I ...

  3. (java)Jsoup爬虫学习--获取网页所有的图片,链接和其他信息,并检查url和文本信息

    Jsoup爬虫学习--获取网页所有的图片,链接和其他信息,并检查url和文本信息 此例将页面图片和url全部输出,重点不太明确,可根据自己的需要输出和截取: import org.jsoup.Jsou ...

  4. 【Java】Jsoup爬虫,一个简单获取京东商品信息的小Demo

    简单记录 - Jsoup爬虫入门实战 数据问题?数据库获取,消息队列中获取中,都可以成为数据源,爬虫! 爬取数据:(获取请求返回的页面信息,筛选出我们想要的数据就可以了!) 我们经常需要分析HTML网 ...

  5. JSOUP爬虫示例

    利用JSOUP做爬虫,爬取我博客中的所有标题加链接,代码示例如下: package com.test.jsoup; import java.io.IOException; import org.jso ...

  6. HttpClient&Jsoup爬虫的简单应用

    详细的介绍已经有很多前辈总结,引用一下该篇文章:https://blog.csdn.net/zhuwukai/article/details/78644484 下面是一个代码的示例: package ...

  7. 利用jsoup爬虫工具,爬取数据,并利用excel导出

    import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.FileInputStream; i ...

  8. Jsoup爬虫解析

    需要下载jsoup-1.8.1.jar包 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQue ...

  9. jsoup爬虫,项目实战,欢迎收看

    import com.mongodb.BasicDBObject import com.mongodb.DBCollection import org.jsoup.Jsoup import org.j ...

随机推荐

  1. LoadRunner内部结构(转)

    LoadRunner内部结构 1,            被测系统是由驱动进程mdrv.exe(多线程驱动的进程)和r3vuser.exe来产生压力的,其中r3vuser.exe仿真应用程序的客户端, ...

  2. SQLAlchemy应用到Flask中

    安装模块 pip install Flask-SQLAlchemy 加入Flask-SQLAlchemy第三方组件 from flask import Flask # 导入Flask-SQLAlche ...

  3. 40.Unique Binary Search Trees(不同的二叉搜索树)

    Level:   Medium 题目描述: Given n, how many structurally unique BST's (binary search trees) that store v ...

  4. linux的CentOS、Ubuntu、Debian三个比较异同

    Linux有非常多的发行版本,从性质上划分,大体分为由商业公司维护的商业版本与由开源社区维护的免费发行版本.商业版本以Redhat为代表,开源社区版本则以debian为代表.这些版本各有不同的特点,在 ...

  5. html input标签 要求只能输入纯数字

    在input标签添加以下代码即可 oninput = "value=value.replace(/[^\d]/g,'')" <input type="text&qu ...

  6. windows开机自启mysql服务(任务计划程序+XAMPP)

    需求:windows开机自启mysql服务 的需求: 相关工具:win10系统中,使用windows自带的任务计划程序 和 XAMPP软件 完成此需求 XAMPP软件介绍:此软件维护了windows中 ...

  7. java中数据库和VO的一一对应关系

    如图所示,数据库中数据如果有下划线,则JavaVO中删除,除第一个单词外,其他单词首字母大写

  8. Jmeter 将正则表达式提取的参数传给全局(跨线程组使用变量)

    一.使用正则表达式提取sessionId 1.在测试计划(跨线程组使用变量)--> 线程组(登录)--> 添加HTTP请求(登录接口) (1)创建测试计划: 勾选独立运行每个线程组(例如在 ...

  9. 本地代码上传至git仓库

    1.进入项目文件夹,初始化 git init 2.添加文件到版本库 git add . 3.提交文件 git commit -m "初次提交" 4.关联远程仓库 git remot ...

  10. 查看tomcat的版本号

    本经验主要介绍在windows下,如何查看tomcat的版本号. 工具/原料 安装了tomcat server的操作系统. 一.绿色版tomcat版本查看--命令catalina version 或者 ...