Write a very simple web crawler in Java

Source: Internet
Author: User

You can set the maximum number of webpages to crawl and automatically add webpage links.

 

Package COM. hudson. test; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. file; <br/> Import Java. io. filewriter; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstreamreader; <br/> Import java.net. httpurlconnection; <br/> Import java.net. URL; <br/> Import Java. util. arraylist; <br/> Import Java. util. hashset; <br/> Import Java. util. list; <br/> Import Java. util. set; <br/>/* <br/> * web crawler 1.0.0 <Br/> * problem: Sometimes the character set cannot be obtained, leading to a storage webpage Encoding Error <br/> * Author: hudson <br/> */<br/> public class webcrawler {<br/> private list <string> URLs; <br/> private set <string> finishedurls; <br/> private int count; <br/> private string filedir; <br/> private int Max; </P> <p> Public webcrawler () {<br/> URLs = new arraylist <string> (); <br/> finishedurls = new hashset <string> (); <br/> COUNT = 0; <br/> filedir = "C: //"; <br/> max = 1 00; <br/>}< br/>/* <br/> * capture a webpage and return a webpage string <br/> */<br/> Public String accessurl (string URL) {<br/> stringbuffer sb = new stringbuffer (); <br/> try {<br/> URL u = new URL (URL ); <br/> httpurlconnection huc = (httpurlconnection) U. openconnection (); <br/> huc. setconnecttimeout (3000); <br/> huc. setreadtimeout (3000); <br/> string charset = getcharset (huc. getcontenttype (); // get the character set, but often cannot get it <br/> bufferedreader br = NULL; <br/> If (! "". Equals (charset) <br/> Br = new bufferedreader (New inputstreamreader (huc. getinputstream (), charset); <br/> else <br/> Br = new bufferedreader (New inputstreamreader (huc. getinputstream (); </P> <p> huc. connect (); <br/> string TMP = NULL; <br/> while (TMP = BR. readline ())! = NULL) {<br/> Sb. append (TMP); <br/>}< br/> huc. disconnect (); <br/> Br. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> string content = sb. tostring (); <br/> int Index = 0; <br/> while (Index = content. indexof ("href = /""))! =-1) {<br/> content = content. substring (index + 6); <br/> string tmpurl = content. substring (0, content. indexof ("/" "); <br/> If (tmpurl. startswith ("HTTP") <br/> URLs. add (tmpurl); <br/>}< br/> count ++; <br/> return sb. tostring (); <br/>}< br/>/* <br/> * obtain the character set of the webpage <br/> */<br/> Public String getcharset (string S) {<br/> string contenttype = s; <br/> string [] values = contenttype. split (";"); <br/> string charset = ""; <Br/> for (string value: values) {<br/> value = value. trim (); <br/> If (value. tolowercase (). startswith ("charset =") {<br/> charset = value. substring ("charset = ". length (); <br/>}< br/> return charset; <br/>}< br/> Public void addurl (string URL) {<br/> URLs. add (URL); <br/>}< br/> Public void setfiledir (string DIR) {<br/> filedir = dir; <br/>}< br/> Public void setmax (INT m) {<br/> max = m; <br />}< Br/>/* <br/> * cyclically crawl until the counter reaches the maximum value <br/> */<br/> Public void work () {<br/> while (count <Max &&! URLs. isempty () {<br/> string url = URLs. get (0); <br/> If (finishedurls. contains (URL) {<br/> URLs. remove (0); <br/> continue; <br/>}< br/> Save (accessurl (URL); <br/> URLs. remove (0); <br/> finishedurls. add (URL ); <br/>}< br/>/* <br/> * store webpage content <br/> */<br/> Public void save (string s) {<br/> try {<br/> file F = new file (filedir); <br/> If (! F. exists () {<br/> F. mkdirs (); <br/>}< br/> filewriter fw = new filewriter (filedir + String. valueof (count) + ". TXT ", false); <br/> FW. write (s); <br/> FW. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> Public static void main (string [] ARGs) {<br/> webcrawler WC = new webcrawler (); <br/> WC. setfiledir ("C: // mytest //"); <br/> WC. addurl ("http://www.hao123.com"); <br/> WC. work (); <br/> system. out. println ("finished"); </P> <p >}< br/>

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.