Find page elements and output to pinned files

Source: Internet
Author: User

ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportOrg.jsoup.Jsoup;Importorg.jsoup.nodes.Document;Importorg.jsoup.nodes.Element;Importorg.jsoup.select.Elements; Public classHreftest {/*** Output File Method *@paramFilePath *@paramSB *@throwsIOException*/     Public Static voidOuthref (String filepath,stringbuffer SB)throwsioexception{Try{File File=NewFile (FilePath); PrintWriter writer=NewPrintWriter (Newfileoutputstream (file));             Writer.write (Sb.tostring ());                     Writer.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); }    }    /*** Positioning elements *@paramargs*/     Public Static voidMain (string[] args) {String FilePath= "/users/liuqi/desktop/result.log"; FinalString url= "http://www.baidu.com/"; String Str=""; StringBuffer SB=NewStringBuffer (); 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+ "\ n"); System.out.println (Link.attr ("href"));            Outhref (FilePath, SB); }                    } Catch(IOException e) {e.printstacktrace (); }            }    }

Need to load Jsoup-1.10.1.jar this jar

Find page elements and output to pinned files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.