Package com.test; ?? Import java.io.*; Import java.net.*; Import java.util.regex.*; ?? Public class Baidulogo { ???? static string geturlcontentstring (String urlstring) throws Exception { ???????? String result = ""; ???????? URL url = new URL (urlstring); ???????? URLConnection urlconnection = url.openconnection (); ???????? Urlconnection.connect (); ???????? InputStreamReader inputstreamreader = new inputstreamreader ( ???????????????? Urlconnection.getinputstream (), "Utf-8"); ???????? BufferedReader in = new BufferedReader (inputstreamreader); ???????? String Line; ???????? While (line = in . ReadLine ()) = null) { ???????????? result + = line; ???????? } ???????? return result; ???? } ?? ???? static string Getlogourl (String contentstring, String patternstring) { ???????? String logourl = null; ???????? Pattern pattern = pattern.compile (patternstring); ???????? Matcher Matcher = Pattern.matcher (contentstring); ???????? if (Matcher.find ()) { ???????????? Logourl = Matcher.group (1); ???????? } ???????? return logourl; ?? ???? } ?? ???? Public staticvoid main (string[] args) throws Exception { ???????? // define the link that will be visited ???????? String urlstring = "http://www.baidu.com"; ???????? String result = geturlcontentstring (urlstring); ???????? String patternstring = "Src=\" (. +?) \""; ???????? String contentstring = result; ???????? String Logourl = Getlogourl (contentstring, patternstring); ???????? System.out.println (logourl); ???? } } |