c爬虫抓取网页数据(java爬虫网络爬虫的注释及应用详细注释)
优采云 发布时间: 2021-09-27 01:20c爬虫抓取网页数据(java爬虫网络爬虫的注释及应用详细注释)
Java爬虫爬取网页数据一. 爬虫简介
网络爬虫,又称网络蜘蛛或网络信息采集器,是一种根据一定的规则自动抓取或下载网络信息的计算机程序或自动化脚本。它是当前搜索引擎的重要组成部分。
我的demo是基于Jsoup做一个java爬虫的简单实现
jsoup是一个Java HTML解析器,主要用于解析HTML jsoup中文官网
二. 必需的 pom.xml 依赖项
org.jsoup
jsoup
1.8.3
commons-io
commons-io
2.5
org.apache.httpcomponents
httpclient
4.5.5
三.java代码(附详细注释)
因为我这里是一个简单的java爬虫,所以只用了一个java
抓取图片和CSS样式并下载到本地
捕捉图像
<p>package cn.xxx.xxxx;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Date 2019/11/20 14:50
* @Version 1.0
*/
public class CatchImage {
// 地址
private static final String URL = "xxxx";
// 编码
private static final String ECODING = "utf-8";
// 获取img标签正则
private static final String IMGURL_REG = "]*?>";
// 获取src路径的正则
private static final String IMGSRC_REG = "(?x)(src|SRC|background|BACKGROUND)=('|\")/?(([\\w-]+/)*([\\w-]+\\.(jpg|JPG|png|PNG|gif|GIF)))('|\")";
// img本地保存路径
private static final String SAVE_PATH = "";
/**
* @param url 要抓取的网页地址
* @param encoding 要抓取网页编码
* @return
*/
public static String getHtmlResourceByUrl(String url, String encoding) {
URL urlObj = null;
URLConnection uc = null;
InputStreamReader isr = null;
BufferedReader reader = null;
StringBuffer buffer = new StringBuffer();
// 建立网络连接
try {
urlObj = new URL(url);
// 打开网络连接
uc = urlObj.openConnection();
// 建立文件输入流
isr = new InputStreamReader(uc.getInputStream(), encoding);
// 建立缓存导入 将网页源代码下载下来
reader = new BufferedReader(isr);
// 临时
String temp = null;
while ((temp = reader.readLine()) != null) {// 一次读一行 只要不为空就说明没读完继续读
// System.out.println(temp+"\n");
buffer.append(temp + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关流
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return buffer.toString();
}
/**
* 获取网页代码保存到本地
*
* @param url 网络地址
* @param encoding 编码格式
*
*/
public static void getJobInfo(String url, String encoding) {
// 拿到网页源代码
String html = getHtmlResourceByUrl(url, encoding);
try {
File fp = new File("xxxxx");
//判断创建文件是否存在
if (fp.exists()) {
fp.mkdirs();
}
OutputStream os = new FileOutputStream(fp); //建立文件输出流
os.write(html.getBytes());
os.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 下载图片
*
* @param listImgSrc
*/
public static void Download(List listImgSrc) {
int count = 0;
try {
for (int i = 0; i