• 网络爬虫技术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();
            }
        }
    }
  • 相关阅读:
    cocos2d-x 屏幕适配
    C# 做一个指定概率的抽奖程序
    Cocos2d-x 开发小记(二):控件
    Cocos2d-x 开发小记(一):基本动作
    Cocos2d-x v2.2.2版本+Win7+VS2010环境搭建
    使用C#从XML中批量删除指定节点
    使用NSIS脚本制作一个安装包
    C++解析命令行参数(仿C语言args)
    关于 Source Engine 2007 网络通信的分析
    关于OpenGL游戏全屏模式的设置
  • 原文地址:https://www.cnblogs.com/h-c-g/p/10683924.html
Copyright © 2020-2023  润新知