Jsoup,java Crawler Solutions, Chinese documents: JsoupI have to say that the ecology of Java is really good, originally I thought the crawler can only use Pyhton to write, the results found that Java Crawler framework not too much ... One minute you can write a simple crawler
WebMagic in Action
But personally think Jsoup best use, the most direct is also very simple
Wrote a demo, climbed the pen fun Net novel, the format has been filtered.
Public classCrawltext {/*** * Get text * *@paramautodownloadfile * Automatically download files *@paramMultithreading * Multithreading default False *@paramURL * Website link *@throwsIOException*/ Public Static voidGetText (BooleanAutodownloadfile,BooleanMultithreading, String Url)throwsIOException {String rule= "Abs:href"; List<String> urllist =NewArraylist<string>(); Document Document=Jsoup.connect (URL). Timeout (4000). Ignorecontenttype (true). useragent ("Mozilla\" to \ "mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) "). get (); System.out.println (Document.tostring ()); Elements Urlnode= Document.select ("a[href$=.html]"); for(Element element:urlnode) {urllist.add (element.attr (rule)); } crawtextthread Crawtextthread=NewCrawtextthread (urllist); Crawtextthread.start (); }}
PackageXyz.yangchaojie.JSOUP.service;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.RandomAccessFile;Importjava.util.List;ImportOrg.jsoup.Jsoup;Importorg.jsoup.nodes.Document; Public classCrawtextthreadextendsThread {List<String>urllist; PublicCrawtextthread (list<string>urllist) { This. Urllist =urllist; } String Rule= ""; String Rule_title= "H1"; String rule_content= "Content"; Public StaticString PATH = "d:\\jsoup\\"; /*** Create file * *@paramFileName *@return */ Public Static voidCreateFile (File fileName)throwsException {Try { if(!filename.exists ()) {Filename.createnewfile (); } } Catch(Exception e) {e.printstacktrace (); } } Public Static voidWritetxtfile (String content, File fileName)throwsException {randomaccessfile mm=NULL; FileOutputStream o=NULL; Try{o=NewFileOutputStream (fileName); O.write (Content.getbytes ("UTF-8")); O.close (); } Catch(Exception e) {e.printstacktrace (); } finally { if(mm! =NULL) {mm.close (); } }} @Override Public voidrun () {CurrentThread (). SetName ("Don't run One:"); String title; String content; for(String url:urllist) {Try{Document Document= Jsoup.connect (URL). Timeout (6000). get (); Title= Document.select ("H1"). toString (); Content= Document.select ("#content"). html (); System.out.println ("Thread:" +currentthread (). GetName () + "Crawl url->" +URL); File File=NewFile (Path+title.replaceall ("); CreateFile (file); System.out.println ("Create File:" +File.getpath ()); Writetxtfile (fileterhtml (content), file); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } } } Public Staticstring fileterhtml (String str) {returnStr.replaceall ("", ""). ReplaceAll ("<br>", "\ r \ n"); }}
Public Static void Main (string[] args) { try { crawltext.gettext (truetrue, "/HTTP/ www.biquge.com.tw/0_66/"); Catch (IOException e) { // TODO auto-generated catch block E.printstacktrace (); } }
RUN:
Create file: D:\JSOUP\ about the new book, here's an account.. txt thread: Don't run: Crawl url->http://www.biquge.com.tw/0_66/83312.htmlTo create a file: D:\JSOUP\ The first chapter frustrated husband. txt thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83313.htmlCreate File: D:\JSOUP\ second chapter hanging the neck scholar. txt thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83314.htmlCreate File: D:\JSOUP\ Chapter III Qin's family tert-txt thread: Don't run: Crawl url->http://www.biquge.com.tw/0_66/83315.htmlCreate file: D:\JSOUP\ fourth calamities. TXT thread: Don't run: Crawl url->http://www.biquge.com.tw/0_66/83316.htmlCreate file: D:\JSOUP\ Fifth Tour mange. TXT thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83317.htmlCreate file: D:\JSOUP\ The sixth chapter of the Jian.. txt thread: Don't run: Crawl url->http://www.biquge.com.tw/0_66/83318.htmlCreate file: D:\JSOUP\ The seventh chapter of self-made (up). txt thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83319.htmlCreate a file: D:\JSOUP\ the eighth chapter of self-made (under). TXT thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83320.htmlCreate a file: D:\JSOUP\ the Nineth Chapter Jiangnan genius. TXT thread: Don't run: Crawl url->http://www.biquge.com.tw/0_66/83321.htmlCreate a file: D:\JSOUP\ the tenth chapter of talent recruit money.. txt thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83322.htmlCreate file: D:\JSOUP\ The 11th chapter is popular in Jiangnan.. txt thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83323.htmlCreate file: D:\JSOUP\ the 12th chapter of the True Colors.. txt thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83324.htmlCreate file: D:\JSOUP\ 13th du home crisis. TXT thread: Don't run: Crawl url->http://www.biquge.com.tw/0_66/83325.htmlCreate file: D:\JSOUP\ 14th culmination. txt thread: Don't run: Crawl url->http://www.biquge.com.tw/0_66/83326.htmlCreate file: D:\JSOUP\ 15th to dissolve crisis (UP). txt thread: Do not run: Crawl url->http://www.biquge.com.tw/0_66/83327.htmlCreate file: D:\JSOUP\ 16th to dissolve crisis (medium). txt
Java Crawler Framework | Crawling fiction