zoukankan      html  css  js  c++  java
  • 网络爬虫技术Jsoup

    Jsoup介绍:
    Jsoup 是一个 Java 的开源HTML解析器,可直接解析某个URL地址、HTML文本内容

    Jsoup主要有以下功能:
    1. 从一个URL,文件或字符串中解析HTML
    2. 使用DOM或CSS选择器来查找、取出数据
    3. 对HTML元素、属性、文本进行操作
    4. 清除不受信任的HTML (来防止XSS攻击)

    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.9.2</version>
    </dependency>
    public class JsoupDemo {
        private static OutputStream os;
     
        public static void main(String[] args) {
            try {
                Document doc = Jsoup.connect("https://www.csdn.net/").get();
    //            System.out.println(doc.title()); //CSDN-专业IT技术社区
                //把文章标题和连接写入txt文件
                Element feedlist_id = doc.getElementById("feedlist_id");
                Elements h2 = feedlist_id.select("h2.csdn-tracking-statistics");
                Elements a = h2.select("a");
                //指定文件名及路径
                File file = new File("E:\jsoup\word\test.txt"); 
                if (!file.exists()) {
                    file.createNewFile();
                }
                //写入本地
                PrintWriter pw = new PrintWriter("E:\jsoup\word\test.txt","UTF-8"); 
                for (Element element : a) {
                    pw.println(element.text());
                    pw.println(element.attr("href")); 
                    pw.println("------------------------------------------------------------------------------------------------------------------------------------");
                }
                pw.close(); //关闭输出流
                //获取页面上的图片保存到本地
                Elements imgs = doc.select("img[src$=.png]");
                for (Element element : imgs) {
                    String img = element.attr("src");
                    String url = "http:"+img;
                    System.out.println(url);
                    System.out.println(url.indexOf("csdn"));
                    if (url.indexOf("csdn")==-1) {
                        continue;
                    }
                    URL u = new URL(url);
                    URLConnection uc=u.openConnection();
                    //获取数据流
                    InputStream is=uc.getInputStream();
                    //获取后缀名
                    String imageName = img.substring(img.lastIndexOf("/") + 1,img.length());
                    //写入本地
                    os = new FileOutputStream(new File("E:\jsoup\img", imageName));
                    byte[] b = new byte[1024];
                    int i=0;
                    while((i=is.read(b))!=-1){
                      os.write(b, 0, i);
                    }
                    is.close();
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    ZZNU 1995: cots' times
    网站后缀名都有哪些
    webstorm运行到服务器(Apache)
    window系统下node.js环境配置
    window系统安装node.js
    网站创建自定义百度地图
    响应式一级到三级导航
    H5插入视频兼容各大浏览器
    phpStudy环境安装
    jquery on和bind
  • 原文地址:https://www.cnblogs.com/h-c-g/p/10683924.html
Copyright © 2011-2022 走看看