zoukankan      html  css  js  c++  java
  • 对于CSDN博客文章不能爬取的问题

    转载 http://blog.csdn.net/rongyongfeikai2/article/details/7826057 

    看过Robin的一篇文章,就是反爬虫的。他提到了几种反爬虫的方法:1.手工拒绝,即爬虫的并发量相当高,那么按照80端口进行并发排序,然后手动的把爬虫的IP给禁掉。2.根据User-Agent拒绝,比如如果我们用Java程序进行爬取时,如果没有设header的话,User-Agent就是java,那么就禁掉User-Agent不为浏览器那样的请求。3.根据流量统计和日志分析来屏蔽爬虫,封掉流量特别大的爬虫。4.实时屏蔽,即如果一个IP在一段时间内请求特别频繁,就为爬虫,加入黑名单,不再响应后续请求。

    高并发的爬虫却是会对网站的服务器造成很大的压力,但是有时候我们需要从ITEYE或者CSDN上爬取一些东西时,也被拒绝掉了。(CSDN博客爬取时报403拒绝请求)

    很明显,1,3,4,条对我们无效,因为我们的爬取不是高并发的频繁的;第2条,User-Agent的判别,才是封掉我们爬取的真正原因。那么,我们就只能加入头部,让自己的爬取像是一个浏览器请求的样子。那么浏览器请求时,发出的是怎样的数据包呢?

    我们可以写个程序在10086端口监听一下(端口你自己随便取):

    1. package com.JavaUtil.IESimilator;  
    2. import java.io.*;  
    3. import java.net.*;  
    4. import java.util.*;  
    5. public class IEHeaderTest {  
    6.   
    7.     //在端口10086上监听,得到IE发送的数据包  
    8.     public IEHeaderTest() {  
    9.           
    10.         int port = 10086;  
    11.         ServerSocket serverSocket = null;  
    12.         Socket client = null;  
    13.         BufferedInputStream bis = null;  
    14.           
    15.         try{  
    16.               
    17.             serverSocket = new ServerSocket(port);  
    18.             client = serverSocket.accept();  
    19.             bis = new BufferedInputStream(client.getInputStream());  
    20.             int index = -1;  
    21.             byte[] buffer = new byte[1024];  
    22.               
    23.             while((index=bis.read(buffer))!=-1){  
    24.                   
    25.                 System.out.println(new String(buffer,0,index));  
    26.             }  
    27.         }catch(Exception ex){  
    28.               
    29.             ex.printStackTrace();  
    30.         }finally{  
    31.               
    32.             if(bis!=null){  
    33.                 try {  
    34.                     bis.close();  
    35.                 } catch (IOException e) {  
    36.                     // TODO Auto-generated catch block  
    37.                     e.printStackTrace();  
    38.                 }  
    39.             }  
    40.             if(client!=null){  
    41.                   
    42.                 try {  
    43.                     client.close();  
    44.                 } catch (IOException e) {  
    45.                     // TODO Auto-generated catch block  
    46.                     e.printStackTrace();  
    47.                 }  
    48.             }  
    49.             if(serverSocket!=null){  
    50.                   
    51.                 try {  
    52.                     serverSocket.close();  
    53.                 } catch (IOException e) {  
    54.                     // TODO Auto-generated catch block  
    55.                     e.printStackTrace();  
    56.                 }  
    57.             }  
    58.         }  
    59.     }  
    60.       
    61.     public static void main(String[] args) {  
    62.           
    63.         IEHeaderTest headerTest = new IEHeaderTest();  
    64.     }  
    65. }  

    然后再在浏览器中输入:http://localhost:10086/ 我们就可以得到IE发送的信息:

    GET / HTTP/1.1
    Accept: image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*
    Accept-Language: zh-CN
    User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C)
    Accept-Encoding: gzip, deflate
    Host: localhost:10086
    Connection: Keep-Alive

    我们可以看到,只有我们把User-Agent设置好,就不会出现爬取被拒绝的问题了:

    1. package com.JavaUtil.IESimilator;  
    2. import java.io.BufferedInputStream;  
    3. import java.io.BufferedReader;  
    4. import java.io.IOException;  
    5. import java.io.InputStreamReader;  
    6. import java.io.UnsupportedEncodingException;  
    7. import java.util.ArrayList;  
    8. import java.util.List;  
    9. import org.apache.commons.httpclient.Header;  
    10. import org.apache.commons.httpclient.HttpClient;  
    11. import org.apache.commons.httpclient.HttpException;  
    12. import org.apache.commons.httpclient.cookie.CookiePolicy;  
    13. import org.apache.commons.httpclient.methods.GetMethod;  
    14. import org.apache.commons.httpclient.params.DefaultHttpParams;  
    15. /* 
    16.  * author:Tammy Pi 
    17.  */  
    18. public class IESimilatorFetchCSDN {  
    19.   
    20.     private HttpClient httpClient = new HttpClient();  
    21.     private GetMethod getMethod = null;  
    22.     private BufferedReader bis = null;  
    23.     private String rtn  = null;  
    24.       
    25.     //get page  
    26.     public String getPage(String url){  
    27.           
    28.         StringBuilder sb = new StringBuilder();  
    29.           
    30.         getMethod = new GetMethod(url);  
    31.         //set http header  
    32.         List<Header> headers = new ArrayList<Header>();  
    33.         headers.add(new Header("Accept"," image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*"));  
    34.         headers.add(new Header("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C)"));  
    35.         headers.add(new Header("Connection","Keep-Alive"));  
    36.   
    37.         //设置Cookie,解决cookie reject问题  
    38.         DefaultHttpParams.getDefaultParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);  
    39.           
    40.         httpClient.getHostConfiguration().getParams().setParameter("http.default-headers",headers);  
    41.           
    42.         try {  
    43.             //设置编码格式  
    44.             int status = httpClient.executeMethod(getMethod);  
    45.             System.out.println("status:"+status);  
    46.               
    47.             bis = new BufferedReader(new InputStreamReader(getMethod.getResponseBodyAsStream(),getMethod.getResponseCharSet()));  
    48.             String line = null;  
    49.             while((line=bis.readLine())!=null){  
    50.                   
    51.                 sb.append(line);  
    52.             }  
    53.               
    54.             try {  
    55.                 rtn = new String(sb.toString().getBytes(getMethod.getResponseCharSet()),"utf-8");  
    56.             } catch (UnsupportedEncodingException e) {  
    57.                 // TODO Auto-generated catch block  
    58.                 e.printStackTrace();  
    59.             }  
    60.         } catch (HttpException e) {  
    61.             // TODO Auto-generated catch block  
    62.             e.printStackTrace();  
    63.         } catch (IOException e) {  
    64.             // TODO Auto-generated catch block  
    65.             e.printStackTrace();  
    66.         } finally{  
    67.               
    68.             if(getMethod!=null){  
    69.                   
    70.                 getMethod.releaseConnection();  
    71.             }  
    72.         }  
    73.           
    74.         return rtn;  
    75.     }  
    76.       
    77.     public static void main(String[] args){  
    78.           
    79.         IESimilatorFetchCSDN similator = new IESimilatorFetchCSDN();  
    80.         String rtn = similator.getPage("http://blog.csdn.net/hdhtqq/article/details/6088461");  
    81.         System.out.println(rtn);  
    82.     }  
    83. }  
  • 相关阅读:
    js 生成 yyyy-mm-dd 格式的逼格姿势
    使用chrome联调不在同一个域的请求
    linux命令收集
    spring component-scan filter
    命令别名alias设置
    vi命令的使用
    Git中的文件状态和使用问题解决
    Git常用命令
    Maven
    MySQL命令
  • 原文地址:https://www.cnblogs.com/chenying99/p/2729920.html
Copyright © 2011-2022 走看看