Java obtains the URL content and the address of the image with the regular expression matching link.

Source: Internet
Author: User

Import java.net .*;
Import java. util. arraylist;
Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import java. Io .*;

Public class questionresult {
Private Socket socket;
Private string host = "www.01hr.com ";
Private int Port = 80;
Private string filepath = "E: // index.txt ";
Private arraylist urllist, imglist;

// Create a socket Link
Public void initsocket () throws exception {
Socket = new socket (host, Port );
}
// Method 1
Public void gethttpurlcontent () throws exception {
URL url = new URL ("http://www.01hr.com ");
Inputstream input = URL. openstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (input ));
String data;
File file = new file (filepath );
Filewriter fw = new filewriter (File );
Bufferedwriter BW = new bufferedwriter (FW );
While (Data = Br. Readline ())! = NULL ){
Matchurl (data );
Matchimage (data );
Bw. Write (data );
Bw. newline ();
}
Bw. Close ();
FW. Close ();
BR. Close ();
Input. Close ();

}

// Method 2
Public void gethttpsocketcontent () throws exception {
Stringbuffer sb = new stringbuffer ("get" + "/index.html"
+ "HTTP/1.1/R/N ");
SB. append ("Host:" + host + "/R/N ");
SB. append ("accept: */R/N ");
SB. append ("Accept-language: ZH-CN/R/N ");
SB. append ("Accept-encoding: gzip, deflate/R/N ");
SB. append ("User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)/R/N ");
SB. append ("connection: keep-alive/R/n/R/N ");

Outputstream socketout = socket. getoutputstream ();
Socketout. Write (sb. tostring (). getbytes ());

Inputstream socketin = socket. getinputstream ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (socketin ));

File file = new file (filepath );
Filewriter fw = new filewriter (File );
Bufferedwriter BW = new bufferedwriter (FW );

String data;
Int I = 0;
While (Data = Br. Readline ())! = NULL ){
If (I> 9) {// The returned message is not displayed
Matchurl (data );
Matchimage (data );
Bw. Write (data );
Bw. newline ();
}
I ++;
}

Bw. Close ();
FW. Close ();
BR. Close ();
Socketin. Close ();
Socketout. Close ();
Socket. Close ();
}
// Match the link content
Public void matchurl (string Str ){
Urllist = new arraylist ();
String RegEx = "<.*? Href = /"(.*?) /"";
Pattern PAT = pattern. Compile (RegEx );
Matcher MAT = Pat. matcher (STR );
While (mat. Find ()){
Urllist. Add (mat. Group (1 ));
}
}
// Match the image content
Public void matchimage (string Str ){
Imglist = new arraylist ();
String RegEx = "Pattern PAT = pattern. Compile (RegEx );
Matcher MAT = Pat. matcher (STR );
While (mat. Find ()){
Imglist. Add (mat. Group (1 ));
}
}
// Print the matched address
Public void printcontent (string contentname, arraylist Al ){
System. Out. println (contentname );
For (INT I = 0; I <Al. Size (); I ++ ){
System. Out. println (Al. Get (I ));
}
}

Public static void main (string ARGs []) throws exception {
Questionresult client = new questionresult ();
/* Client. initsocket (); // method 2
Client. gethttpsocketcontent ();*/
Client. gethttpurlcontent (); // method 1
Client. printcontent ("--------------- print link address ---------------", client. urllist );
Client. printcontent ("--------------- print the image address -------------", client. imglist );
}

}

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.