Java for simple web crawling

Source: Internet
Author: User

Requirement Description: Use Java to crawl Web page information and return it as a string.

Implemented using Java code:

 PackageNet.ibuluo.spider.util;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.io.Reader;Importjava.net.MalformedURLException;ImportJava.net.URL;/*** HTTP Tool *@authorRobin Zhang **/ Public classHttputil {/*** Crawl Web page information according to the URL and return it as a string *@paramurlstr * URL String *@return     * @throwsmalformedurlexception*/     Public Staticstring GetUrl (String urlstr) {string result=NULL; Try{URL URL=NewURL (URLSTR); Result=inputstream2string (Url.openstream ()); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }                returnresult; }            /*** Read the information in the byte stream and convert to a string *@paramInputStream * Byte stream to read *@return     * @throwsIOException*/    Private StaticString inputstream2string (inputstream inputstream)throwsioexception{Reader Reader=NULL; StringBuilder Builder=NULL; Try{            //Stream bytes to a character flowReader =NewInputStreamReader (InputStream); //Creating a String containerBuilder =NewStringBuilder (); //set character stream read length            Char[] buffer =New Char[1024]; //records the length of each read, primarily to record the length of the last read            intOffset = 0;  while((offset=reader.read (buffer)) > 0){                //converts the read content into a string and puts it into the builderBuilder.append (NewString (buffer, 0, offset)); }            returnbuilder.tostring (); } Catch(IOException e) {e.printstacktrace (); }finally{            if(NULL!=reader)            {Reader.close (); }        }        return NULL; }             Public Static voidMain (string[] args) {System.out.println (GETURL ("http://www.ibuluo.net/") ); }}

The above content can be implemented using a third-party plug-in Jsoup. Use the Jsoup implementation code as follows:

    try  {            = Jsoup.connect ("http://www.baidu.com/"). get ();            System.out.println (doc.html ());         Catch (IOException e) {            e.printstacktrace ();        }

Jsoup is more useful in implementing the analysis of HTML documents. You can refer to the official website of Jsoup.

Java for simple web crawling

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.