Parse an html webpage and read the specified body (go to news ads)

Source: Internet
Author: User

// This example uses the text of Sina news as an example.
@ Testpublic boolean getContent (String link) throws Exception {// get the corresponding input stream URL = new url (link) through the link; URLConnection urlConnection = URL. openConnection (); InputStream inputStream = urlConnection. getInputStream (); // Save the stream to a byte [] array contentByte byte [] contentByte = new byte [1024*10]; byte [] buffer = new byte [1024]; int I =-1; while (I = inputStream. read (buffer ))! =-1) {contentByte = byteMerger (contentByte, buffer);} // convert the array into a string, and then add, delete, modify, and query the string. note the encoding when converting byte [] arrays into strings. It must be consistent with the link source encoding String str = new String (contentByte, "gb2312 "); // The following is the truncation of the string int start = str. indexOf ("
 "); Int end = str. indexOf ("
 "); If (start! =-1) {// if the search is successful, that is, the source file has the target to be intercepted. // The String contentStr = str to be truncated. substring (start, end); // Add necessary html code. The main conversion formats must also be one-to-one. the added html encoding must be consistent with byte [] write1 ="
 ". GetBytes (" GB2312 "); byte [] write = contentStr. getBytes (" GB2312 "); byte [] write2 ="". GetBytes (); OutputStream outputStream = new FileOutputStream (Environment. getExternalStorageDirectory (). toString () + "/tmp.html"); // use the byteMerger method to connect to the byte [] array outputStream. write (byteMerger (write1, write), write2); outputStream. close (); return true;} else {// if it fails, save the source file. check the cause of the Error. FileWriter fw = new FileWriter (Environment. getExternalStorageDirectory (). toString () + "/aa.txt"); fw. flush (); fw. write (str); fw. close (); return false ;}}// java merges two byte Arrays: public static byte [] byteMerger (byte [] byte_1, byte [] byte_2) {byte [] byte_3 = new byte [byte_1.length + byte_2.length]; System. arraycopy (byte_1, 0, byte_3, 0, byte_1.length); System. arraycopy (byte_2, 0, byte_3, byte_1.length, byte_2.length); return byte_3 ;}

Related Article

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.