Java captures webpage content and obtains the specified server IP Address

Source: Internet
Author: User
Code:
  1. Package ttwork.net;
  2. Import java.net .*;
  3. Import java. util. RegEx .*;
  4. Import java. Io .*;
  5. Public class webpage {
  6. Public static void main (string [] ARGs ){
  7. // Http://zhidao.baidu.com/question/192012139.html
  8. Webpage Wp = new webpage ("http://tieba.baidu.com/F? KZ = 664340519 ");
  9. // WP. getsubhtml (1, 20, "src/f2.html ");
  10. WP. getinnerhtml ("/^ ([// w-//.] +) @ (// [[0-9] {1, 3 }//. [0-9] {1, 3 }//. [0-9] {1, 3 }//.) | ([// w-] + //.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (//]?) $/G "," src/f2.html ");
  11. }
  12. Private URL; // URL
  13. Private inetaddress [] IP address; // ip address
  14. Private file desfile; // target file
  15. Private inputstreamreader ISR; // Reader
  16. Private outputstreamwriter OSW; // writer
  17. Private bufferedreader BR; // Reader
  18. Private bufferedwriter bw; // writer
  19. /**
  20. * @ Param protocol name
  21. * @ Param host name or domain name
  22. */
  23. Public webpage (string _ URL ){
  24. Try {
  25. Url = new URL (_ URL );
  26. Try {
  27. IP = inetaddress. getallbyname (_ URL. substring (7 ));
  28. } Catch (unknownhostexception e ){
  29. IP = NULL;
  30. }
  31. } Catch (malformedurlexception e ){
  32. E. printstacktrace ();
  33. }
  34. }
  35. /**
  36. * Obtain the IP address of a specified website. Some websites have more than one IP address.
  37. * @ Return the IP address of the string connection
  38. */
  39. Public String getallip (){
  40. String ipall = NULL;
  41. For (INT I = 0; I <IP. length; I ++ ){
  42. Ipall + = IP [I]. tostring () + "/N ";
  43. }
  44. Return ipall;
  45. }
  46. /**
  47. * Obtain all the content on the webpage.
  48. */
  49. Public void gethtml (string desfilepath ){
  50. Try {
  51. Desfile = new file (desfilepath );
  52. ISR = new inputstreamreader (URL. openstream ());
  53. OSW = new outputstreamwriter (New fileoutputstream (desfile ));
  54. Char [] line = new char [1024];
  55. While (ISR. Read (line)> 0 ){
  56. OSW. Write (line );
  57. OSW. Flush ();
  58. }
  59. } Catch (filenotfoundexception e ){
  60. E. printstacktrace ();
  61. } Catch (ioexception e ){
  62. E. printstacktrace ();
  63. } Finally {
  64. Try {
  65. OSW. Close ();
  66. ISR. Close ();
  67. } Catch (ioexception e ){
  68. E. printstacktrace ();
  69. }
  70. }
  71. }
  72. /**
  73. * Match by regular expression to obtain the content in the specified format, for example, to obtain all email addresses on the webpage
  74. * Verify the regular expression of the email address.
  75. */^ ([/W-/.] +) @ (/[0-9] {1, 3 }/. [0-9] {1, 3 }/. [0-9] {1, 3 }/.) | ([/W-] + /.) +) ([A-Za-Z] {2, 4} | [0-9] {1, 3}) (/]?) $/G
  76. */
  77. Public void getinnerhtml (string RegEx, string desfilepath ){
  78. Try {
  79. Desfile = new file (desfilepath );
  80. ISR = new inputstreamreader (URL. openstream ());
  81. BR = new bufferedreader (ISR );
  82. OSW = new outputstreamwriter (New fileoutputstream (desfile ));
  83. BW = new bufferedwriter (OSW );
  84. Pattern P = pattern. Compile (RegEx );
  85. Matcher m;
  86. String line;
  87. While (line = Br. Readline ())! = NULL ){
  88. M = P. matcher (line );
  89. If (M. Find ()){
  90. Int S = M. Start ();
  91. Int e = M. End ();
  92. Bw. Write (line. substring (S, e ));
  93. Bw. Flush ();
  94. Bw. newline ();
  95. }
  96. }
  97. } Catch (filenotfoundexception e ){
  98. E. printstacktrace ();
  99. } Catch (ioexception e ){
  100. E. printstacktrace ();
  101. } Finally {
  102. Try {
  103. Bw. Close ();
  104. OSW. Close ();
  105. BR. Close ();
  106. ISR. Close ();
  107. } Catch (ioexception e ){
  108. E. printstacktrace ();
  109. }
  110. }
  111. }
  112. /**
  113. * Get the content from the specified row on the webpage to the specified row. According to Java principles, the content includes the startrow line, not the endrow line.
  114. *
  115. */
  116. Public void getsubhtml (INT startrow, int endrow, string desfilepath ){
  117. Try {
  118. Int rownum = 0;
  119. Desfile = new file (desfilepath );
  120. ISR = new inputstreamreader (URL. openstream ());
  121. BR = new bufferedreader (ISR );
  122. OSW = new outputstreamwriter (New fileoutputstream (desfile ));
  123. BW = new bufferedwriter (OSW );
  124. String line;
  125. While (line = Br. Readline ())! = NULL ){
  126. Rownum ++;
  127. If (rownum <startrow ){
  128. Continue;
  129. } Else if (rownum> = startrow & rownum <endrow ){
  130. Bw. Write (line );
  131. Bw. Flush ();
  132. Bw. newline ();
  133. } Else {
  134. Break;
  135. }
  136. }
  137. } Catch (filenotfoundexception e ){
  138. E. printstacktrace ();
  139. } Catch (ioexception e ){
  140. E. printstacktrace ();
  141. } Finally {
  142. Try {
  143. Bw. Close ();
  144. OSW. Close ();
  145. BR. Close ();
  146. ISR. Close ();
  147. } Catch (ioexception e ){
  148. E. printstacktrace ();
  149. }
  150. }
  151. }
  152. }

 

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.