java抓取网页内容(pom中引入jsoup代码jsoup的一些方法获取元素的方法 )

优采云 发布时间: 2021-10-14 09:23

  java抓取网页内容(pom中引入jsoup代码jsoup的一些方法获取元素的方法

)

  文章内容

  有时需要获取网页图片。一般有以下几种方式:

  1、Python 是最方便的,但它有学习成本。

  2、图片批量下载软件,这个有,但是不支持多页,爬取规则也不是很灵活。

  3、java不是最有效率的抢手方式,但是专业是java,所以没有学习成本。

  在 pom.xml 中引入 jsoup

  

org.jsoup

jsoup

1.9.1

  代码

  /**

* 1、STORE_LOCATION 是图片存储路径,这个根据需要自定义

* 2、抓取图片的需求不同,规则自定义下

* 3、多网页抓取,用个for循环即可。

*/

public class DownloadImageUtils {

private static Logger logger = LoggerFactory.getLogger(DownloadImageUtils.class);

public static String STORE_LOCATION="F:\\pic\\";

public static List images =new ArrayList();

public static void main(String[] args) {

String netUrl = "http://www.baidu.com"; //要爬的网页

new DownloadImageUtils().start(netUrl);

}

public void start(String pageUrl){

List images = parsePage(pageUrl);

downloadImage(images);

}

public List parsePage(String pageUrl){

List images =new ArrayList();

Connection connect = Jsoup.connect(pageUrl);

Document document = null;

try {

document = connect.get();

/* 自定义规则 */

Elements elements = document.getElementsByTag("img"); // 找到所有img标签

for (Element element : elements) {

String alt = element.attr("alt");

String src = element.attr("src");

Image image = new Image();

if(!StringUtils.isEmpty(alt) && src.contains("imgs") && !src.contains("imgs2")){

System.out.println(element.html());

System.out.println(element.attr("src"));

image.setAlt(alt.replace("-FHD",""));

image.setSrc(src);

images.add(image);

}

}

} catch (IOException e) {

e.printStackTrace();

}

return images;

}

//下载该图片!

public void downloadImage(List images){

for (Image image:images) {

InputStream in = null;

BufferedOutputStream os = null;

try {

URL url = new URL(image.getSrc());

File file = new File(STORE_LOCATION + image.getAlt() + ".jpg");

logger.info("filename:{}",file.getAbsoluteFile());

if(file.exists()){ //

return;

}

URLConnection conn = url.openConnection();

in = conn.getInputStream();

os = new BufferedOutputStream(new FileOutputStream(file.getAbsoluteFile()));

byte[] buff = new byte[1024];

int num = 0;

while((num = in.read(buff))!= -1)

{

os.write(buff, 0, num);

os.flush();

}

} catch (MalformedURLException e) {

logger.error("获取图片url异常");

e.printStackTrace();

} catch (IOException e) {

logger.error("下载图片url连接异常");

e.printStackTrace();

}

finally{

if( in != null){

try {

in.close();

} catch (IOException e) {

logger.error("读入流关闭异常");

}

}

if( os != null){

try {

os.close();

} catch (IOException e) {

logger.error("输出流关闭异常");

}

}

}

}

}

static class Image {

private String alt;

private String src;

public String getAlt() {

return alt;

}

public void setAlt(String alt) {

this.alt = alt;

}

public String getSrc() {

return src;

}

public void setSrc(String src) {

this.src = src;

}

}

}

  jsoup的一些方法

  jsoup 有不止一种获取元素的方式,代码:

  // 根据标签获取元素

document.getElementsByTag("img");

// 根据css样式获取元素

document.query(".red");

  URLConnection 和 jsoup

  也可以使用 URLConnection 来读取网页内容。但是解析的时候只能拼字符串,还是很头疼的。 jsoup 更方便。

  URL myurl = new URL(netUrl);

URLConnection myconn = myurl.openConnection();

InputStream myin = myconn.getInputStream();

mybr = new BufferedReader(new InputStreamReader(myin,"UTF-8"));

String line;

while((line = mybr.readLine())!= null)

{

getImageUrl(line,netUrl);//判断网页中的jpg图片

}

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线