java 爬蟲詳解及簡單實(shí)例
Java爬蟲
一、代碼
爬蟲的實(shí)質(zhì)就是打開網(wǎng)頁源代碼進(jìn)行匹配查找,然后獲取查找到的結(jié)果。
打開網(wǎng)頁:
URL url = new URL(http://www.cnblogs.com/Renyi-Fan/p/6896901.html);
讀取網(wǎng)頁內(nèi)容:
BufferedReader bufr = new BufferedReader(new InputStreamReader(url.openStream()));
正則表達(dá)式進(jìn)行匹配:
tring mail_regex = "\\w+@\\w+(\\.\\w+)+";
儲(chǔ)存結(jié)果:
List<String> list = new ArrayList<String>();
/*
* 獲取
* 將正則規(guī)則進(jìn)行對象的封裝。
* Pattern p = Pattern.compile("a*b");
* //通過正則對象的matcher方法字符串相關(guān)聯(lián)。獲取要對字符串操作的匹配器對象Matcher .
* Matcher m = p.matcher("aaaaab");
* //通過Matcher匹配器對象的方法對字符串進(jìn)行操作。
* boolean b = m.matches();
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Spider {
public static void main(String[] args) throws IOException {
// List<String> list = getMails();
// for(String mail : list){
// System.out.println(mail);
// }
List<String> list = getMailsByWeb();
for(String mail : list){
System.out.println(mail);
}
}
public static List<String> getMailsByWeb() throws IOException{
//1,讀取源文件。
//URL url = new URL("http://192.168.1.100:8080/myweb/mail.html");
//URL url = new URL("http://localhost:8080/SecondWeb/index.jsp");
URL url = new URL("http://www.cnblogs.com/Renyi-Fan/p/6896901.html");
BufferedReader bufr = new BufferedReader(new InputStreamReader(url.openStream()));
//2,對讀取的數(shù)據(jù)進(jìn)行規(guī)則的匹配。從中獲取符合規(guī)則的數(shù)據(jù).
String mail_regex = "\\w+@\\w+(\\.\\w+)+";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile(mail_regex);
String line = null;
while((line=bufr.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
//3,將符合規(guī)則的數(shù)據(jù)存儲(chǔ)到集合中。
list.add(m.group());
}
}
return list;
}
public static List<String> getMails() throws IOException{
//1,讀取源文件。
BufferedReader bufr = new BufferedReader(new FileReader("c:\\mail.html"));
//2,對讀取的數(shù)據(jù)進(jìn)行規(guī)則的匹配。從中獲取符合規(guī)則的數(shù)據(jù).
String mail_regex = "\\w+@\\w+(\\.\\w+)+";
List<String> list = new ArrayList<String>();
Pattern p = Pattern.compile(mail_regex);
String line = null;
while((line=bufr.readLine())!=null){
Matcher m = p.matcher(line);
while(m.find()){
//3,將符合規(guī)則的數(shù)據(jù)存儲(chǔ)到集合中。
list.add(m.group());
}
}
return list;
}
}
二、運(yùn)行結(jié)果
abc1@sina.com.cn 1@1.1
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
關(guān)于HashSet與HashMap的區(qū)別及說明
這篇文章主要介紹了關(guān)于HashSet與HashMap的區(qū)別及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Activiti7通過代碼動(dòng)態(tài)生成工作流實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Activiti7通過代碼動(dòng)態(tài)生成工作流實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Spring?Boot自動(dòng)配置的原理及@Conditional條件注解
這篇文章主要介紹了Spring?Boot自動(dòng)配置的原理及@Conditional條件注解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2022-07-07
Spring中的@ControllerAdvice三種用法詳解
這篇文章主要介紹了Spring中的@ControllerAdvice三種用法詳解,加了@ControllerAdvice的類為那些聲明了(@ExceptionHandler、@InitBinder或@ModelAttribute注解修飾的)方法的類而提供的<BR>專業(yè)化的@Component,以供多個(gè)Controller類所共享,需要的朋友可以參考下2024-01-01
SpringBoot中短時(shí)間連續(xù)請求時(shí)出現(xiàn)Cookie獲取異常問題的解決方案
在 Spring Boot Web 應(yīng)用中,每個(gè)請求都會(huì)攜帶 HttpServletRequest,其中包含 Cookie 等關(guān)鍵信息,如果某個(gè)請求對象的 cookieParsed 標(biāo)記在異步線程中被錯(cuò)誤修改,可能會(huì)導(dǎo)致 短時(shí)間內(nèi)的后續(xù)請求無法正確解析 Cookie,本文給大家介紹了詳細(xì)解決方法,需要的朋友可以參考下2025-04-04

