|
Package catch_url;
Import java. io. BufferedReader;
Import java. io. InputStreamReader;
Import java.net. URL;
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class Urls {
Private String startUrl; // start to collect the URL
String urlContent;
String ContentArea;
Private String strAreaBegin, strAreaEnd; // The collection area starts to collect strings and ends to collect strings.
@ SuppressWarnings ("unused ")
Private String stringInUrl, stringNotInUrl;
String strContent; // The collected content.
String [] allUrls; // All URLs collected
Private String regex; // collection rules
UrlAndTitle urlAndTitle = new UrlAndTitle (); // store the URL and title
Public static void main (String [] args)
{
Urls myurl = new Urls ("<body", "/body> ");
Myurl. getStartUrl ("http://www.bKjia. c0m /");
Myurl. getUrlContent ();
Myurl. getContentArea ();
Myurl. getStringInUrl ("http://www.bKjia. c0m /");
Myurl. getStringNotInUrl ("google ");
Myurl. Urls ();
}
// Initialize the constructors strAreaBegin and strAreaEnd.
Public Urls (String strAreaBegin, String strAreaEnd ){
This. strAreaBegin = strAreaBegin;
This. strAreaEnd = strAreaEnd;
}
//
Public void Urls ()
{
Int I = 0;
// String regex = "<
// Href = "? '? Http: // [a-zA-Z0-9] +. [a-zA-Z0-9] +. [a-zA-Z] + /? [.? [S | s] + [a>] $ ";
Final String regex = "<.*? /A> ";
// String regex = "http: //. *?> ";
Final Pattern pt = Pattern. compile (regex );
Final Matcher mt = pt. matcher (ContentArea );
While (mt. find ()){
System. out. println (mt. group ());
I ++;
// Obtain the title
Final Matcher title = Pattern. compile ("> .*? </A> "). matcher (mt. group ());
While (title. find ()){
System. out. println ("title :"
+ Title. group (). replaceAll (">|</a> ",""));
}
// Obtain the URL
Final Matcher myurl = Pattern. compile ("href =. *?> "). Matcher (mt. group ());
While (myurl. find ()){
System. out. println ("url :"
+ Myurl. group (). replaceAll ("href = |> ",""));
}
System. out. println ();
}
System. out. println ("Total" + I + "matching results ");
}
// Obtain the start collection URL
Public void getStartUrl (String startUrl)
{
This. startUrl = startUrl;
}
// Obtain the content of the website;
Public void getUrlContent ()
{
StringBuffer is = new StringBuffer ();
Try {
URL myUrl = new URL (startUrl );
BufferedReader br = new BufferedReader (new InputStreamReader (myUrl
. OpenStream ()));
String s;
While (s = br. readLine ())! = Null ){
Is. append (s );
}
UrlContent = is. toString ();
} Catch (Exception e)
{
System. out. println ("failed to output URL File ");
E. printStackTrace ();
}
}
// Obtain the matching area of the website
Public void getContentArea ()
{
Int pos1 = 0, pos2 = 0;
Pos1 = urlContent. indexOf (strAreaBegin) + strAreaBegin. length ();
Pos2 = urlContent. indexOf (strAreaEnd, pos1 );
ContentArea = urlContent. substring (pos1, pos2 );
}
// The following two functions obtain the keywords that the website should contain and those that cannot be included
// Here, we only conduct preliminary experiments. In the later stage, there should be more than one protected keyword and non-contained keyword.
Public void getStringInUrl (String stringInUrl)
{
This. stringInUrl = stringInUrl;
}
Public void getStringNotInUrl (String stringNotInUrl)
{
This. stringNotInUrl = stringNotInUrl;
}
// Obtain collection rules
// Obtain the url
Public void getUrl ()
{
}
Public String getRegex ()
{
Return regex;
}
Class UrlAndTitle {
String myURL;
String title;
}
} |