JAVA刷CSDN部落格流量的思路

來源:互聯網
上載者:User

標籤:img   不同   redirect   catch   使用   cli   技術分享   11.2   csdn部落格   

先來看看CSDN統計部落格訪問量的原理:

當用戶端向伺服器發起請求的時候,每個ip被統計一次,的三個用戶端指的是三個不同的ip地址。並且在某段時間內,同以ip訪問同一篇文章,只能算是一次訪問量。同一ip在某個時間段內訪問同一部落格不同的文章,被訪問到的文章次數+1。知道上訴簡單的原理之後,接下來就想解決的辦法了。

首先,準備一大批ip代理。一百度,謔出來一大堆啊,這個年代雷鋒還是挺多的,我選擇了這家ip代理網站,並提供了api的調用介面,調用地址是:

http://www.xicidaili.com/api。

其次,引入我們需要的jar包,這裡只需要一個jsoup包,千萬別被jsoup這個名詞嚇到,下面我會貼出我的部落格,說說這個哥們幹嘛用的。

  1. jsoup-1.8.1.jar

接下來寫一個封裝ip類的java bean,其實不寫bean也可以,直接放在String[]數組裡面也是ok的,不過這樣做的時候,代理調用的時候方便取出。

public class MyIp {    private String address;        private String port;    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }    public String getPort() {        return port;    }    public void setPort(String port) {        this.port = port;    }}

  現在主要用到的調用這個介面的工具是:jsoup,這個哥們我推薦看我的部落格http://blog.csdn.net/zhang5476499/article/details/52205150。當然你也可以用httpClient等工具把網頁的內容拿下來。用java 代碼實現如下:

public static List<MyIp> getIp(String url) {        List<MyIp> ipList = null;        try {            //1.向ipProxy 位址發起get請求,拿到代理的ip            Document doc = Jsoup.connect(url)              .userAgent("Mozilla")              .cookie("auth", "token")              .timeout(3000)              .get();                        //2,將得到的ip位址解析除字串            String ipStr = doc.body().text().trim().toString();                        //3.用Regex去切割所有的ip            String[] ips = ipStr.split("\\s+");                        //4.迴圈遍曆得到的ip字串,封裝成MyIp的bean            ipList = new ArrayList<MyIp>();            for(final String ip : ips) {                MyIp myIp = new MyIp();                String[] temp = ip.split(":");                myIp.setAddress(temp[0].trim());                myIp.setPort(temp[1].trim());                ipList.add(myIp);            }        } catch (IOException e) {            System.out.println("載入文檔出粗");        }         return ipList;    }

  這裡需要注意的是,api調用這個頁面的 時候,使用的是get請求。好了,假設已經拿到了api給的100個代理的ip,那麼,我們就可以設定代理了,設定代理的主要代碼如下:

System.setProperty("http.maxRedirects", "50"); System.getProperties().setProperty("proxySet", "true"); System.getProperties().setProperty("http.proxyHost", myIp.getAddress());System.getProperties().setProperty("http.proxyPort", myIp.getPort());

  下一步的工作是:迴圈這些拿到的代理ip,每設定一次代理,向部落格地址發一次請求。

 1 public static void main(String[] args) { 2         //1.想httpProxy 位址api發起請求,獲得想要的代理ip地址 3         String url = "http://api.xicidaili.com/free2016.txt"; 4         List<MyIp> ipList = getIp(url); 5         String blogUrl = ""; 6         Scanner sc = new Scanner(System.in); 7          8         System.out.print("請輸入文章地址:"); 9         blogUrl = sc.nextLine();10         System.out.print("請輸入輪番重新整理的倍數(x100):");11         int time = sc.nextInt();12         int count = 0;13         14         for(int i = 0; i< time; i++) {15             //2.設定ip代理16             for(final MyIp myIp : ipList) {17                 System.setProperty("http.maxRedirects", "50");18                 System.getProperties().setProperty("proxySet", "true");19                 System.getProperties().setProperty("http.proxyHost", myIp.getAddress());20                 System.getProperties().setProperty("http.proxyPort", myIp.getPort());21                 22                 try {23                     Document doc = Jsoup.connect(blogUrl)24                                       .userAgent("Mozilla")25                                       .cookie("auth", "token")26                                       .timeout(3000)27                                       .get();28                     if(doc != null) {29                         count++;30                         logger.info("成功重新整理次數: " + count);31                     }32                 } catch (IOException e) {33                     logger.error(myIp.getAddress() + ":" + myIp.getPort() + "報錯");34                 }35             }36         }37     }
代理寫到這差不多就結束了,其實刷流量也不需要多高深的技術,本文只是一個引子,希望能夠祈禱拋磚引玉的作用。相關代碼地址:http://download.csdn.net/detail/zhang5476499/9687448 --2016.11.27 14:08:30

JAVA刷CSDN部落格流量的思路

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.