• 查找网页元素并且输出到固定文件


    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    public class HrefTest {
        
       /**
        * 输出文件方法
        * @param filePath
        * @param sb
        * @throws IOException
        */
        public static void outHref(String filePath,StringBuffer sb) throws IOException{
            
            try {
                File file = new File(filePath);
                PrintWriter writer = new PrintWriter(new FileOutputStream(file));  
                 writer.write(sb.toString());
                 writer.close();
                 
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        /**
         * 定位元素
         * @param args
         */
    
        public static void main(String[] args) {
            String  filePath="/Users/liuqi/Desktop/result.log";
            final String url="http://www.baidu.com/";
            String str="";
            StringBuffer sb=new StringBuffer();
            try {
                Document doc=Jsoup.connect(url).get();
                Elements links=doc.getElementsByTag("a");
                for(Element link:links){
                    if(link.attr("href").contains("baidu"))
                         str=link.attr("href").toString();
                         sb.append(str+"
    ");
                         System.out.println(link.attr("href"));
                         outHref(filePath, sb);
                }
                
            } catch (IOException e) {
                e.printStackTrace();
            }
            
    
        }
        
    
    }

    需要加载jsoup-1.10.1.jar这个jar

  • 相关阅读:
    swiper把左右箭头放到外侧
    vue复制地址栏链接:clipboard
    移动端适配rem.js
    vue登录弹框
    vue使用vue-aplayer(还用了SiriWave波形动画插件)
    vue 锚点定位,并平滑过渡
    vue引入scss报错:scss版本太高
    Kylin |3.Cube的优化
    Kylin |2.ModuleProject
    算法| 高级动态规划
  • 原文地址:https://www.cnblogs.com/liuqi/p/6795271.html
Copyright © 2020-2023  润新知