URL類(Package java.net)
主要用於將用戶所輸入的網絡地址進行分割。
主要方法:1.構造方法:public URL(String spec) throws MalformedURLException
2.public String getFile();//獲取文件名;
3.public int getPort();//獲取端口;
4.public String getProtocol();//獲取協議;
5.public String getHost();//獲取主機;
6.public URLConnection openConnection() throws IOException;//獲取URLConnection對象,該對象對Socket類又進行了封裝。
代碼示例:使用以上方法;
package second.study; import java.net.MalformedURLException; import java.net.URL; public class Test { public static void main(String[] args) throws Exception { URLDemo(); } private static void URLDemo() throws MalformedURLException { URL url = new URL("http://192.168.2.158:10002/myweb/Demo.html?name=haha"); System.out.println("getProtocol = " + url.getProtocol()); System.out.println("getPath = " + url.getPath()); System.out.println("File = " + url.getFile()); System.out.println("getQuery = " + url.getQuery()); } } /* Res: * getProtocol = http * getPath = /myweb/Demo.html * File = /myweb/Demo.html?name=haha * getQuery = name=haha */
URLConnection(Package java.net)
將Socket對象進行了封裝,並對收到的傳輸層數據進行拆包到應用層。簡化代碼的書寫。
package second.study; import java.io.IOException; import java.net.URL; public class Test { public static void main(String[] args) throws Exception { openConnection(); } private static void openConnection() throws IOException { URL url = new URL("http://192.168.2.158:10002/myweb/Demo.html?name=haha"); System.out.println("openConnection = " + url.openConnection()); } } /* Res: * openConnection = * sun.net.www.protocol.http.HttpURLConnection:http://192.168.2.158:10002/myweb/Demo.html?name=haha */
正則表達式
符合一定規則的表達式。專門用於操作字符串,並簡化對字符串的復雜操作。注意:字符串中一旦出現\,就要出現兩次(\\)。
為了讓規則的結果能被重用,可以按照將該規則封裝成一個組,用()完成,每個組都有編號,從1開始,想要使用已有的組可以通過\n(n就是組的編號)的形式來獲取。
$n:獲取前一個正則表達式中的第n組。
具體操作功能:
匹配:String類中的:public boolean matches(String regex);
切割:String類中的:public String[] split(String regex);
替換:String類中的:public String replaceAll(String regex, String replacement);
獲取:將字符串中符合規則的子串取出。
步驟:1.將正則表達式封裝成對象;
2.讓正則對象和要操作的字符串相關聯;
3.關聯后,獲取正則匹配引擎;
4.通過引擎對符合規則的子串進行操作,比如取出。
Class Pattern(Package java.util.regex)
主要方法:1.public static Pattern compile(String regex);//將正則表達式封裝成對象
2.public Matcher matcher(CharSequence input);//將該對象與字符串關聯,返回匹配器;
3.Matcher類中的public boolean find();//尋找匹配內容;
4.Matcher類中的public String group();//獲取匹配內容;//find和group方法相當於迭代器。
網絡爬蟲簡單代碼示例
package second.study; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 需求:獲取指定互連網上的郵件地址。 * 思路: * 函數1:getInputStream獲取指定瀏覽器中的數據:返回InputStream * 1.使用URL對象連接上指定的互聯網; * 2.使用URL對象中的URLConnection返回URLConnection應用層對象; * 3.使用URLConnection對象中的InputStream獲取該瀏覽器中的輸入流獲取文字; * 函數2:將字節流轉換為字符流,並和相應的正則表達式匹配,存入TreeSet集合,避免重復字符串。 * 1.使用bufferedReader InputStreamReader 對象,將字節流轉換為字符流; * 2.使用Pattern對象將正則表達式進行編譯,和輸入流進行關聯,返回匹配器對象 * */ public class Test { public static void main(String[] args) throws Exception { getMails(); } private static void getMails() throws IOException { InputStream inputStream = getInputStream("https:****"); TreeSet<String> set = getSetFromIPAdd(inputStream); for(String str : set) { System.out.println(str); } } /** * 將匹配的結果存入到集合中 * @param inputStream * @return * @throws IOException */ private static TreeSet<String> getSetFromIPAdd(InputStream inputStream) throws IOException { // 1.創建集合 TreeSet<String> set = new TreeSet<String>(); // 創建正則表達式 String regex = "\\w+@\\w+(\\.\\w+)+"; // 將正則表達式編譯 Pattern pattern = Pattern.compile(regex); // 將字節流轉換為字符流 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String len = null; while((len = bufferedReader.readLine()) != null) { // 將字符流和正則表達式關聯 Matcher matcher = pattern.matcher(len); // 運行匹配器 while(matcher.find()) { set.add(matcher.group()); } } // 關閉流資源 inputStream.close(); return set; } /** * 連接互聯網指定的InetAdd地址,返回連接成功后的輸入流。 * @param InetAdd : 互聯網地址; * @return 輸入流 * @throws IOException */ private static InputStream getInputStream(String InetAdd) throws IOException { // 1.使用URL對象連接上指定的互聯網; URL url = new URL(InetAdd); // 2.使用URL對象中的URLConnection返回URLConnection應用層對象; URLConnection connection = url.openConnection(); // 3.使用URLConnection對象中的InputStream獲取該瀏覽器中的輸入流獲取文字; InputStream inputStream = connection.getInputStream(); return inputStream; } }
