Java crawls hospital information on a website

Source: Internet
Author: User

Crawls hospital information on a website and helps one student write it. It is completely sold now, and uses jsoup to parse the returned HTML code.

Httprequestproxy. Java

Import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import java.net. httpurlconnection; import java.net. URL; import Java. util. arraylist; import Java. util. list; import Java. util. imports; import Java. util. regEx. matcher; import Java. util. regEx. pattern; import Java. io. fileoutputstream; import Java. io. outputstreamwrite R; import org.html parser. util. parserexception; import Org. jsoup. jsoup; import Org. jsoup. nodes. document; import Org. jsoup. nodes. element; import Org. jsoup. select. elements; public class httprequestproxy {Private Static list <mzinfo> mzinfos = new arraylist <mzinfo> (); Private Static list <mzinfo> levelinfo = new arraylist <mzinfo> (); private Static list <mzinfo> cityinfo = new arraylist <mzinfo> (); public static St Ring getwebcontent (string urlstring, final string charset, int timeout) throws ioexception {If (urlstring = NULL | urlstring. length () = 0) {return NULL;} urlstring = (urlstring. startswith ("http: //") | urlstring. startswith ("https ://"))? Urlstring: ("http: //" + urlstring ). intern (); Url url = new URL (urlstring); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestproperty ("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; Trident/4.0 ;. net CLR 1.1.4322 ;. net CLR 2.0.50727) "); Conn. setrequestproperty ("accept", "text/html"); Conn. setconnecttimeout (timeout); try {If (Conn. getresponsecode ()! = Httpurlconnection. http_ OK) {return NULL ;}} catch (ioexception e) {e. printstacktrace (); return NULL;} inputstream input = Conn. getinputstream (); bufferedreader reader = new bufferedreader (New inputstreamreader (input, charset); string line = NULL; stringbuffer sb = new stringbuffer (); While (line = reader. readline ())! = NULL) {sb. append (Line). append ("\ r \ n");} If (reader! = NULL) {reader. Close () ;}if (Conn! = NULL) {Conn. disconnect ();} return sb. tostring ();} public static string getwebcontent (string urlstring) throws ioexception {return getwebcontent (urlstring, "iso-8859-1", 5000);} public static void gethospitolinfo (string URL) {int pagessum = 1; string Path = ". /result _ "+ system. currenttimemillis () + ". CSV "; string S = NULL; try {S = getwebcontent (URL); s = new string (S. getbytes ("iso-8859-1"), "utf8");} catch (Ioexception E1) {// todo auto-generated catch blocke1.printstacktrace ();} document DOC = jsoup. parse (s); elements pageele = Doc. select ("Div [class = page]> A"); If (pageele! = NULL &&! "". Equals (pageele. text (). trim () {for (element pages: pageele) {If ("last page ". equals (pages. text () {string lasturl = pages. ATTR ("href"); int A = lasturl. lastindexof (". "); string STR = lasturl. substring (A-3, a); string RegEx = "[^ 0-9]"; pattern P = pattern. compile (RegEx); matcher M = P. matcher (STR); pagessum = integer. parseint (M. replaceall (""). trim (); system. out. println ("data volume:" + pagessum) ;}} fileoutputstream Fos = NULL; o Utputstreamwriter OSW = NULL; bufferedwriter fw = NULL; try {Fos = new fileoutputstream (PATH); OSW = new outputstreamwriter (FOS, "GBK"); fw = new bufferedwriter (OSW ); for (INT I = 1; I <= pagessum; I ++) {system. out. println ("processing number" + I + "processing number"); string FIR = URL. substring (0, URL. lastindexof (". ")-1); s = getwebcontent (FIR + I + ". html "); s = new string (S. getbytes ("iso-8859-1"), "utf8"); Doc = jsoup. parse (s); ELEM Ents ele = Doc. select ("Div [class = part-list]"); For (element: Ele) {String title = element. select ("H4> "). text (); string KeShi = element. select ("P> A [target = _ self]"). text (); string dengji = element. select ("H4 "). text (); int Index = dengji. lastindexof ("("); int last = dengji. lastindexof (")"); // system. out. println (KeShi + "\ t" + title); FW. write (Title + "," + KeShi + "," + dengji. substring (index + 1, last) + "\ N ") ;}} catch (exception e) {e. printstacktrace ();} finally {try {FW. close (); OSW. close (); FOS. close ();} catch (ioexception e) {// todo auto-generated catch blocksystem. out. println ("ioexception") ;}} public static void main (string [] ARGs) throws ioexception, parserexception {While (true) {// initialize the linked data system. out. println ("Get hospital classification information"); gethostlevel (); system. out. println ("classification information is obtained. Enter the Classification Number: "); partition inputlevel = new partition (system. in); int levelbianhao = inputlevel. nextint (); system. out. println ("START initializing region data .... "); mzinfo mzinfolevel = levelinfo. get (levelbianhao); // gethospitolinfo (mzinfolevel. geturl (); string S = getwebcontent (mzinfolevel. geturl (); getnative (s); system. out. println ("Enter region number:"); partition input = new partition (system. in); int bianhao = input. nextint (); system. out. println ("whether to display hospital information in accordance with the City License [Y/n]"); println = new license (system. in); string flag = inputyn. next (); mzinfo = mzinfos. get (bianhao); If ("Y ". endswith (FLAG) {system. out. println ("getting city information"); getcityhospital (mzinfo); system. out. println ("the city information is obtained successfully. Enter the city number"); required inputcity = New Nation (system. in); int citynum = inputcity. nextint (); mzinfo = cityinfo. get (citynum);} system. out. println ("Starting capture information:"); gethospitolinfo (mzinfo. geturl (); system. out. println ("the information is captured successfully, whether to continue [Y/n]"); repeated inputyn = new partition (system. in); string flag2 = inputyn. next (); If ("N ". equals (flag2) {break;} runtime.getruntime(cmd.exe C ("cmd CLS") ;}} Private Static void getcityhospital (mzinfo) throws ioexception {string S = getwebcontent (mzinfo. geturl (); s = new string (S. getbytes ("iso-8859-1"), "utf8"); document DOC = jsoup. parse (s); elements Sf = Doc. select ("Div [class = find-hospital]> H4"); For (element El: SF) {If (mzinfo. getdiqu (). equals (El. select ("H4> "). text () {elements ele = el. select ("H4> div> ul> li"); // system. out. println (mzinfo. getdiqu (); For (INT I = 0; I <ELE. size (); I ++) {element = ELE. get (I); string url = element. select (""). ATTR ("href"); string name = element. select (""). text (); mzinfo mzinfo2 = new mzinfo (); mzinfo2.setdiqu (name); mzinfo2.seturl (URL); cityinfo. add (mzinfo2); system. out. println ("" + I + ":" + name) ;}}} Private Static void getnative (string s) throws ioexception {S = new string (S. getbytes ("iso-8859-1"), "utf8"); document DOC = jsoup. parse (s); elements ele = Doc. select ("Div [class = find-hospital]> H4"); // cyclically saved for (INT I = 0; I <ELE. size (); I ++) {element = ELE. get (I); elements ele1 = element. select ("H4> A"); mzinfo = new mzinfo (); string diqu = ele1.text (); mzinfo. setdiqu (diqu); mzinfo. seturl (ele1.attr ("href "). tostring (); mzinfos. add (mzinfo); system. out. println (I + ":" + diqu) ;}} Private Static void gethostlevel () throws ioexception {// todo auto-generated method stubstring S = getwebcontent ("http://hospital.qqyy.com/list-p110000c0a110108k0v1r0d0n0.html "); S = new string (S. getbytes ("iso-8859-1"), "utf8"); document DOC = jsoup. parse (s); // <H4 class = 'show' id = 'mal'> elements ele = Doc. select ("Div [class = find-orders ments-tab tab2]> span> A"); // For (INT I = 0; I <ELE. size (); I ++) {element = ELE. get (I); system. out. println (I + ":" + element. text (); mzinfo = new mzinfo (); mzinfo. setdiqu (element. text (); mzinfo. seturl (element. ATTR ("href "). tostring (); levelinfo. add (mzinfo );}}}

Mzinfo. Java

public class MZinfo {private String diqu;private String url;public String getDiqu() {return diqu;}public void setDiqu(String diqu) {this.diqu = diqu;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}

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.