zoukankan      html  css  js  c++  java
  • 查找网页元素并且输出到固定文件

    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

  • 相关阅读:
    Crypto++库安装、测试
    Unix环境高级编程(一)
    Unix 环境高级编程
    C++加密解密库之选择
    python简单网页服务器示例
    使用resteasy作为dubbox消费者
    oracle驱动包maven下载失败解决
    dubbox下载编译运行demo
    Linux环境变量从用户配置改为系统配置
    @Override注解在Eclipse中编译报错
  • 原文地址:https://www.cnblogs.com/liuqi/p/6795271.html
Copyright © 2011-2022 走看看