code example
Package Demo0806;import Java.io.bufferedreader;import Java.io.file;import java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.net.URL;import Java.net.urlconnection;import Java.util.hashmap;import Java.util.iterator;import Java.util.Map;import Java.util.map.entry;import Java.util.set;public class Scantitlefromwebpage { private String website ; private map<string,string> recentsharecode=new hashmap<string,string> (); private map<string,string> hotsharecode=new hashmap<string,string> (); public scantitlefromwebpage (String website) { this.website= website; } public String scanwebfortitle () { inputstream inputstream=null; string title=null; &nBsp try { //Create a URL object, for example: Baidu search China good sound //wd keyword value is the UTF-8 code for "Good sound in China", //can use Urlencoder to edit characters url url = new URL (website); & nbsp; //creating URLConnection objects urlconnection openconnection = url.openconnection (); //Some websites do not allow Java as Client access openconnection.setrequestproperty ("User-agent", "mozilla/4.0 (compatible; MSIE 5.0; Windows NT; Digext); //Get page information encoding type string Headerfield = Openconnection.getheaderfield (" Content-type "); int indexOf = Headerfield.indexof (" charset= " ); string encoding = headerfield.substring (indexof+8); // Gets the input stream for the URLConnection object inputstream= Openconnection.getinputstream (); //reading stream via Io, writing to file string line= null; inputstreamreader Inputstreamreader; &nbsP InputStreamReader = new InputStreamReader (inputstream,encoding); bufferedreader BufferedReader = new BufferedReader (inputstreamreader); int flagofrecentsharecode=0; int flagofhotsharecode=0; string recentcode=null; string recenthref=null; string hotcode=null; string hothref=null; while ((Line=bufferedreader.readline ())!=null) { int start=-1; int end=-1; if (( Start=line.indexof ("<title>"))!=-1) { end=line.indexof ("</title>"); title = line.substring (start+7, end); } else if (Line.indexof ("Newcodelist")!=-1) { flagOfRecentShareCode=1; } &nbSp;else if (Line.indexof ("Hotcodelist")!=-1) { flagOfRecentShareCode=0; flagOfHotShareCode=1; } else if (line.indexof ("</div>")!=-1) { flagOfHotShareCode=0; } else if ((start= line.indexof ("href=")!=-1 &&flagofrecentsharecode==1) { end=line.indexof ("target"); recenthref=line.substring (start+6, end-1); } else if ((start= line.indexof ("href=")!=-1&&flagofhotsharecode==1) { end=line.indexof ("target"); hothref=line.substring (start+6, end-1); } else if ((start= line.indexof ("title=")!=-1&& flagofrecentsharecode==1) { end=line.indexof (">"); recentcode=line.substring (start+7, end-1); recentsharecode.put (Recentcode, recenthref); } else if ((start= line.indexof ("title=")!=-1&&flagofhotsharecode==1) { end=line.indexof (">"); hotcode=line.substring (start+7, end-1); hotsharecode.put ( Hotcode, Hothref); } } inputstreamreader.close (); } catch (IOException e) { system.err.println ("Unable to download"); e.printstacktrace (); } finally { if (inputstream!=null) { try { inputstream.close (); } catch (Exception ex) { //do not process } } &NBSP;&NBSP;&NBSp } return title; } public static void Main (string[] args) throws Interruptedexception, IOException { String website= "http://www.oschina.net"; scantitlefromwebpage scantitlefromwebpage; scantitlefromwebpage=new scantitlefromwebpage (website) ; string title = Scantitlefromwebpage.scanwebfortitle (); file file=new File ("oschomepage.html"); fileoutputstream FileOutputStream = new FileOutputStream (file); if (title!=null) { string str= "site title:" +title; byte[] bytes = str.getbytes (); &NBsp;fileoutputstream.write (bytes); fileoutputstream.write (' \ n '); system.out.println (str); } Map<String,String> recentsharecode= scantitlefromwebpage.recentsharecode; Map<String,String> hotsharecode= scantitlefromwebpage.hotsharecode; set<entry<string, string>> entryset; iterator<entry<string, string>> iterator; string key=null; string value=null; entry<string, string> next=null; fileoutputstream.write ("----------------the latest share code has the following----------------". GetBytes ()); & nbsp; fileoutputstream.write (' \ n '); system.out.println ("---- ------------The latest share code has the following----------------"); entryset= Recentsharecode.entryset ( ); iterator= entryset.iterator (); while ( Iterator.hasnext ()) { next = Iterator.next (); key=next.getkey (); value=next.getvalue (); string str=key+ "\ T" + "(" +value + ")"; byte[] bytes = str.getbytes (); fileoutputstream.write (bytes); fileoutputstream.write (' \ n '); &NBSp System.out.println (key+ "\ T" + "(" +value+ "); } fileoutputstream.write ("----------------The hottest code of the week has the following----------------". GetBytes ()); fileoutputstream.write (' \ n '); system.out.println ("------------- -----This week's hottest code has the following-----------------"); entryset= hotsharecode.entryset (); iterator= entryset.iterator (); while ( Iterator.hasnext ()) { next = Iterator.next (); key=next.getkey (); value=next.getvalue (); string str=key+ "\ T" + "(" +value + ")"; byte[] bytes = Str.getbytes ();   fileoutputstream.write (bytes); fileoutputstream.write (' \ n '); System.out.println (key+ "\ T" + "(" +value+ "); } fileoutputstream.close (); }}
Run results
Website title: Open Source China-Find the Open source project you want, share and communicate----------------the latest sharing code has the following----------------IOS a function that returns multiple parameters simultaneously (http://www.oschina.net/ code/snippet_865986_50059) jquery plugin--ajaxfileupload.js (http://www.oschina.net/code/snippet_105637_50057) The Garlic Guest-Challenge Puzzle -6 (http://www.oschina.net/code/snippet_587996_50055) picture lazy loading simple principle (http://www.oschina.net/code/snippet_ 1590754_50058) How do I write a code that can achieve early sleep and early risers? (http://www.oschina.net/code/snippet_1168184_50061) Shell Gets the current script execution absolute path (http://www.oschina.net/code/snippet_ 1988965_50056) Bubble Sort algorithm Java implementation (http://www.oschina.net/code/snippet_587996_50052) JS Script Controls Overview of page-flipping controls. This control is mainly used to turn the page of an effect, if you like can that go to reference, hehe (http://www.oschina.net/code/snippet_1862064_50060) Select the Sorting Method Java implementation (HTTP/ www.oschina.net/code/snippet_587996_50053)-Challenge Puzzle -5 (http://www.oschina.net/code/snippet_587996_50054)-------- ----------this week the most popular code has the following-----------------python implementation of the crawl map, do not climb the sister map, too much (http://www.oschina.net/code/snippet_2371155_ 49889) obtain bank name (http://www.oschina.net/code/snippet_1252640_49997) by bank card number Java Reflection Base, build framework (important) (http://www.oschina.net/ Code/snippet_2345495_49988) Spring boot + mybatis+ spring MVC Integration (http://www.oschina.net/code/snippet_2325859_49871) Full medical pass-HTML5 development, single page integrated version (HTTP ://www.oschina.net/code/snippet_2287693_50012) Public number payment (http://www.oschina.net/code/snippet_1754599_49966) My Eclipse Code formatting style (http://www.oschina.net/code/snippet_1584959_49953) Python simple crawler (http://www.oschina.net/code/ snippet_2391943_49998) Get rival computer photo artifact (http://www.oschina.net/code/snippet_2425035_49995) 12306 train ticket API interface QQ 89914505 ( http://www.oschina.net/code/snippet_811693_49880)
Java implementation simply captures relevant data from open source China home page