Java reads HTML files and extracts a href-related connection and link titles

Source: Internet
Author: User

Import java. util. RegEx. matcher;
Import java. util. RegEx. pattern;
Import java. util .*;
Import java. Io .*;
Import java. NiO. charbuffer;
Public class regtest
{
Public static void main (string [] ARGs)
{

// Define an article list class. Contains the URL and title of the article
Class articlelist {
String URLs;
String title;

Public articlelist (){}
Public articlelist (string T, string U)
{
Title = T;
URLs = u;

}

Public String tostring ()
{
Return ("title:" + title + "url:" + URLs + "");

}
}
// ---------- Start --- read the HTML file to buff --------------------------------------------------------
File file = new file ("C: // bb.html ");
Bufferedreader reader = NULL;
Stringbuffer buff = new stringbuffer ();
Try {
System. Out. println ("read the file content in the unit of action, read the entire row at a time :");
Reader = new bufferedreader (New filereader (File ));
String tempstring = NULL;

Int line = 1;
// Read a row at a time until null is the end of the file.
While (tempstring = reader. Readline ())! = NULL ){
// Display the row number
Buff. append (tempstring + "/R/N ");
// System. Out. println ("line" + LINE + ":" + tempstring );
Line ++;
}
// System. Out. println (buff. tostring ());
Reader. Close ();
} Catch (ioexception e ){
E. printstacktrace ();
} Finally {
If (reader! = NULL ){
Try {
Reader. Close ();
} Catch (ioexception E1 ){
}
}
}

// ------------ End -- read ---------------------------------------------------------------------------

Arraylist <articlelist> Al = new arraylist <articlelist> ();
String S = buff. tostring ();
System. out. println (s); // "</P> <P style = height: 14px> <a href = http://jingjia.baidu.com> Enterprise Promotion </a> | <a href = http://top.baidu.com> Search </a> | <a href =/home.html> About Baidu </> | <a href = http://ir.baidu.com> About Baidu </a> </P> <p id = B> & copy; 2008 Baidu <a href = http://www.baidu.com/duty> before using Baidu must read </A> <a href = http://www.miibeian.gov.cn target = _ blank> Beijing ICP Certificate No. 030173 </a> <a href = http://www.hd315.gov.cn/beian/view.asp? Bianhao = 010202001092500412> </a> </P> </center> </body> String RegEx = "<.*? /A> ";
 
Pattern Pt = pattern. Compile (RegEx );
// System. Out. println (RegEx );
Matcher Mt = pt. matcher (s );
// String includestring = ". * Baidu.com. *"; // The string must contain "Baidu.com"
 
While (mt. Find ())
{
If (true)
{

// System. Out. println (mt. Group ());

String S2 = "> [^ <]. *? [^>] </A> "; // Title
String S3 = "href = /".*? /"";


Pattern pt2 = pattern. Compile (S2 );
Matcher MT2 = pt2.matcher (mt. Group ());
Pattern pt3 = pattern. Compile (S3 );
Matcher mt3 = pt3.matcher (mt. Group ());
While (mt2.find () & mt3.find ())
{
System. Out. println ("title:" + mt2.group (). replaceall (">|</A> ",""));
System. Out. println ("url:" + mt3.group (). replaceall ("href = |> ",""));
String T = mt2.group (). replaceall (">|</A> ","");
String u = mt3.group (). replaceall ("href = |> ","");
Al. Add (New articlelist (t, u ));
}

}

} // End while

For (INT I = 0; I <Al. Size (); I ++)
System. Out. println (Al. Get (I ));

System. Out. println ("Total" + Al. Size () + "result ");
}

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.