http協議下的網絡爬蟲

主管讓做個抓取淘寶數據的功能,但是淘寶的比較難,我先從扒新浪新聞開始。

環境,Apache 提供免費的 HTTPClien t源碼和 JAR 包下載,可以登陸這里下載,筆者用的是4.51版本。

參考apache提供的例子,使用正則表達式做出如下程序。


public class Main {
    
    public static void Detail(String url) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String oldStr;
        try {
            HttpGet httpget = new HttpGet(url);
            String encoding="gbk";
            if(url.contains("comments")){
                
                encoding = "utf-8";
            }
            System.out.println(encoding);
            System.out.println("Executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
          
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity entity = response.getEntity();
                oldStr = EntityUtils.toString(response.getEntity(),encoding);
 
                // Call abort on the request object
                httpget.abort();
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }

        Pattern pattern =  Pattern.compile("<title>[^<]*</title>");
        Matcher matcher = pattern.matcher(oldStr);
        if(matcher.find()){
            String str = matcher.group();
            str = str.substring(7,str.length()-8);
            System.out.println("---"+str);
        }
        
        pattern =  Pattern.compile("<p>[^<]*</p>");
        matcher = pattern.matcher(oldStr);
        while(matcher.find()){
            String str = matcher.group();
            str = str.substring(3,str.length()-4);
            System.out.println(str);
        }

    }

     

    
    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String oldStr;
        try {
            
            String str = null;
        str ="http://news.sina.com.cn/hotnews/";
            HttpGet httpget = new HttpGet(str);
            System.out.println("Executing request " + httpget.getURI());
            CloseableHttpResponse response = httpclient.execute(httpget);
            try { System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity entity = response.getEntity();
                oldStr = EntityUtils.toString(response.getEntity(),"UTF-8");
                // Call abort on the request object
                httpget.abort();
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
        Pattern pattern =  Pattern.compile("href='http://[^']*'");
        Matcher matcher = pattern.matcher(oldStr);
        int i= 1;
        while(matcher.find()){
            String str = matcher.group();
            str = str.substring(6,str.length()-1);
            System.out.println(str);
            Detail(str);
            System.out.println(i++);
        }
    }
}
```
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,846評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,837評論 18 139
  • 33款可用來抓數據的開源爬蟲軟件工具 要玩大數據,沒有數據怎么玩?這里推薦一些33款開源爬蟲軟件給大家。 爬蟲,即...
    visiontry閱讀 7,376評論 1 99
  • 客戶套路深,我要回農村,最近和這幾個客戶周旋都腦子疼,媽蛋英語爛,要去學英文了,這樣套路才能走得更精彩
    地瓜222閱讀 275評論 1 0
  • 在赤壁之戰里有三個國家分別是蜀國,吳國,魏國。其中魏國最為強大他是由曹操掌管的,曹操可是讓人聞風喪膽,而且曹操還有...
    楓林志穎閱讀 681評論 0 0