package com.bettem.commons.core.util;
import cn.hutool.core.io.IoUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
/**
* JsoupUtil
*
* @author liguoliang
* @date 2015年9月23日下午3:02:56
*/
public class JsoupUtil {
/**
* 获取HTML源码
* liguoliang
* 2015年9月23日下午3:02:56
*
* @param httpUrl 图片网络地址
* @param encode 编码
* @return String
*/
public static String getHtmlCode(String httpUrl, String encode) {
try {
URL url = new URL(httpUrl);
// //使用openStream得到一输入流并由此构造一个BufferedReader对象
BufferedReader reader = IoUtil.getReader(url.openStream(), encode);
StringWriter stringWriter = new StringWriter();
IoUtil.copy(reader, stringWriter);
return stringWriter.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取HTML元素信息
* liguoliang
* 2015年9月23日下午3:03:09
*
* @param httpUrl 图片网络地址
* @param filePath 图片保存路径
*/
public static String getHtmlPicture(String httpUrl, String filePath) {
FileOutputStream fos = null;
String fileName;
InputStream in = null;
URL url;
String imageUrl = null;
try {
File imageFile = new File(filePath);
if (!imageFile.exists()) {
imageFile.mkdirs();
}
Document doc = Jsoup.connect(httpUrl).get();
//带src属性的元素
Elements image = doc.select("[src]");
for (Element src : image) {
if ("img".equals(src.tagName())) {
imageUrl = src.attr("src");
System.out.println("图片地址:" + imageUrl);
File file = new File(imageUrl);
fileName = file.getName();
boolean b = (imageUrl.startsWith("http://") || imageUrl.startsWith("https://")) && fileName.contains(".");
if (b) {
url = new URL(imageUrl);
URLConnection connection = url.openConnection();
in = connection.getInputStream();
//删除相同文件名并重新下载
File[] files = imageFile.listFiles();
for (File file2 : files) {
if (file2.getName().equals(fileName)) {
file2.delete();
}
}
File targetFile = new File(filePath + fileName);
fos = new FileOutputStream(targetFile);
IoUtil.copy(in, fos);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
IoUtil.close(in);
IoUtil.close(fos);
}
return imageUrl;
}
/**
* liguoliang
* 2015年9月23日下午7:06:57
*
* @param filePath 保存地址
* @param imageUrl 网络地址
*/
public static void downImag(String filePath, String imageUrl) {
String fileName = imageUrl.substring(imageUrl.lastIndexOf("/"));
URL url;
InputStream in = null;
OutputStream os = null;
try {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
url = new URL(imageUrl);
URLConnection connection = url.openConnection();
in = connection.getInputStream();
File targetPath = new File(filePath + fileName);
os = new FileOutputStream(targetPath);
IoUtil.copy(in, os);
} catch (Exception e) {
e.printStackTrace();
} finally {
IoUtil.close(in);
IoUtil.close(os);
}
}
public static void main(String[] args) {
String httpUrl = "http://www.netbian.com/";
String encode = "gb2312";
// String encode = "gbk";
// String encode = "utf8";
String filePath = "e:/phone/newfile/111/";
String resource = getHtmlCode(httpUrl, encode);
System.out.println(resource);
//方式一:
/*Document document = Jsoup.parse(resource);
Elements element = document.getElementsByTag("img");
for (Element element2 : element) {
String imgUrl = element2.attr("src");
File file = new File(imgUrl);
if (!"".equals(imgUrl) && imgUrl.startsWith("http://") && file.getName().contains(".")) {
System.out.println("正在批量下图片===========================");
downImag(filePath, imgUrl);
System.out.println("图片地址:" + imgUrl);
}
}*/
//方式二:
getHtmlPicture(httpUrl, filePath);
httpUrl = "http://img.netbian.com/file/2020/0710/93b4f00e30f595a020b0e7dc09338154.jpg";
filePath = "e:/phone/newfile";
//图片下载
downImag(filePath,httpUrl);
}
}
分享到:
相关推荐
如果您下载了本程序,但是该程序无法运行,或者您不会部署,那么您可以选择退款或者寻求我们的帮助(如果找我们帮助的话,是需要追加额外费用的) 爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。...
使用Jsoup 实现对网页的解析,能实现对网页代码识别和批量图片下载等功能,使用jsoup 1.7jar,仅供参考学习
使用Jsoup解析csdn主页,并获取博客列表中的博主id和头像。
Google的logo,感觉设计的很好,最近看了java的jsoup(html 解析器)把Google的logo全部下载下来,另外,在Google logos页面发现都是英文的,通过利用Google翻译api对logo说明进行翻译,最终把图片信息以及翻译结果...
这个是用java写的可执行文件,因为一个IP如果爬取次数过多过于频繁是会被封的。...这里是针对于有兴趣研究爬虫的初学者们做的一个,跳过了URL直接面向页面html代码进行代码解析的,然后将图片下载下来的工具。
java 实现简单爬虫,爬取图片。 根据爬取页面内容,使用jsoup解析html页面,获取需要的路径,进行循环下载。 博客:https://blog.csdn.net/qq_37902949/article/details/81257065
如果您下载了本程序,但是该程序无法运行,或者您不会部署,那么您可以选择退款或者寻求我们的帮助(如果找我们帮助的话,是需要追加额外费用的) 爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。...
如果您下载了本程序,但是该程序无法运行,或者您不会部署,那么您可以选择退款或者寻求我们的帮助(如果找我们帮助的话,是需要追加额外费用的) 爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。...
这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。...
4. 内容解析:使用HTML解析器(如Jsoup)解析网页内容,提取需要的信息,如链接、文本、图片等。 5. 链接提取:从解析后的内容中提取出其他链接,用于进一步爬取。 6. 数据存储:将获取到的信息存储到数据库、文件或...
这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。...
这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。...
这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。...
如果您下载了本程序,但是该程序存在问题无法运行,那么您可以选择退款或者寻求我们的帮助(如果找我们帮助的话,是需要追加额外费用的)。另外,您不会使用资源的话(这种情况不支持退款),也可以找我们帮助(需要...
这个是通过Jsoup去获取百度网页的内容,jsoup 是一款 Java 的HTML 解析器,可直接解析某个URL地址、HTML文本内容。它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据,可以看作...
使用httpclient,jsoup,dom4j,json-lib,SWT创建的可下载小说的网络爬虫项目。 爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫...
常用的解析技术包括正则表达式、HTML解析器(如Jsoup)、XPath等。 链接管理:爬虫会管理已抓取的链接,并根据一定的策略进行URL的过滤、去重、排序等操作,以确保抓取效率和数据的完整性。 数据存储:爬虫将抓取...