栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Java

java 爬虫

Java 更新时间:发布时间: 百科书网 趣学号

根据吾爱破解的一个大佬写的之后改了一下,爬的我需要的素材。
测试地址 https://www.loggly.com/docs-index/log-sources/
里面大部分注释也是大佬写的,很简单,和js差不多感觉

 
            org.jsoup
            jsoup
            1.13.1
        
package com.plan.plan;

import org.jsoup.Connection;
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.HttpURLConnection;
import java.net.URL;

public class pc {
    public static void main(String[] args) throws IOException {
        long t1=System.currentTimeMillis();
        //访问目标网址
        Connection connection1=Jsoup.connect("https://www.loggly.com/docs-index/log-sources/");
        //连接成功后获取document对象
        document document1= connection1.get();
        Element elementDiv=document1.selectFirst("[class=log-sources]");

        Element elementDiv1=elementDiv.selectFirst("[class=container]");//搜索class=cl r   标签
        Element elementUL=elementDiv1.selectFirst("[class=row]");
        Element elementUL1=elementUL.selectFirst("[class=col-sm-8]");

        Elements elements=null;
        try {
            Thread.sleep(5000);
             elements=elementUL1.getElementsByClass("log-sources__main");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Elements elementUL1mainlist=elements.select(".log-list");
        Elements elementLis=elementUL1mainlist.select(".log-list__item");//通过找到的ul 搜索ul里面的所有li标签
        for(Element elementLi:elementLis) {//遍历所有找到的li
            Element elementA=elementLi.selectFirst("a");//搜索li里的a标签
            String name=elementA.attr("href");
            Elements elements1log__front=elementA.getElementsByClass("log__front");
            Elements select = elements1log__front.select("img[src]");
            String src2=null;

            String divName= String.valueOf(System.currentTimeMillis());

            for(Element element:select){
                String src=element.attr("abs:src");//获取src的绝对路径
                 src2=element.attr("src");//获取src的绝对路径
           //好像有反爬虫机制 所以加了这个代码
//             参考          https://www.cnblogs.com/xijieblog/p/4540026.html
                URL url=new URL(src2);
                url = new URL(src2);
                HttpURLConnection connection = (HttpURLConnection) url.
                        openConnection();
                connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

                InputStream is=url.openStream();

                FileOutputStream fos=new FileOutputStream("D:\date\test"+"//"+divName+".png");

                byte[] b=new byte[2048];
                int count=is.read(b);
                while(count!=-1) {
                    fos.write(b,0,count);
                    fos.flush();
                    count=is.read(b);
                }
                fos.close();
                is.close();
            }
            }

        long t2=System.currentTimeMillis();
        double a=(t2-t1)/1000;
        System.out.println("下载完毕"+"用时:"+a+"s");
    }
}
转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/273282.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号