最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java代理實(shí)現(xiàn)爬取代理IP的示例

 更新時(shí)間:2018年05月07日 11:13:35   作者:sdfiiiiii  
今天小編就為大家分享一篇java代理實(shí)現(xiàn)爬取代理IP的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧

僅僅使用了一個(gè)java文件,運(yùn)行main方法即可,需要依賴的jar包是com.alibaba.fastjson(版本1.2.28)和Jsoup(版本1.10.2)

如果用了pom,那么就是以下兩個(gè):

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>1.2.28</version>
</dependency>
<dependency>
  <groupId>org.jsoup</groupId>
  <artifactId>jsoup</artifactId>
  <version>1.10.2</version>
</dependency>

完整的代碼如下:

package com.tuniu.fcm.facade.IPProxy;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * 獲取代理IP,需要
 * com.alibaba.fastjson.JSONObject以及Jsoup
 */
public class ProxyCralwerUnusedVPN {
  ThreadLocal<Integer> localWantedNumber = new ThreadLocal<Integer>();
  ThreadLocal<List<ProxyInfo>> localProxyInfos = new ThreadLocal<List<ProxyInfo>>();
  public static void main(String[] args) {
    ProxyCralwerUnusedVPN proxyCrawler = new ProxyCralwerUnusedVPN();
    /**
     * 想要獲取的代理IP個(gè)數(shù),由需求方自行指定。(如果個(gè)數(shù)太多,將導(dǎo)致返回變慢)
     */
    proxyCrawler.startCrawler(1);
  }
  /**
   * 暴露給外部模塊調(diào)用的入口
   * @param wantedNumber 調(diào)用方期望獲取到的代理IP個(gè)數(shù)
   */
  public String startCrawler(int wantedNumber) {
    localWantedNumber.set(wantedNumber);
    kuaidailiCom("http://www.xicidaili.com/nn/", 15);
    kuaidailiCom("http://www.xicidaili.com/nt/", 15);
    kuaidailiCom("http://www.xicidaili.com/wt/", 15);
    kuaidailiCom("http://www.kuaidaili.com/free/inha/", 15);
    kuaidailiCom("http://www.kuaidaili.com/free/intr/", 15);
    kuaidailiCom("http://www.kuaidaili.com/free/outtr/", 15);
    /**
     * 構(gòu)造返回?cái)?shù)據(jù)
     */
    ProxyResponse response = new ProxyResponse();
    response.setSuccess("true");
    Map<String, Object> dataInfoMap = new HashMap<String, Object>();
    dataInfoMap.put("numFound", localProxyInfos.get().size());
    dataInfoMap.put("pageNum", 1);
    dataInfoMap.put("proxy", localProxyInfos.get());
    response.setData(dataInfoMap);
    String responseString = JSONObject.toJSON(response).toString();
    System.out.println(responseString);
    return responseString;
  }
  private void kuaidailiCom(String baseUrl, int totalPage) {
    String ipReg = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3} \\d{1,6}";
    Pattern ipPtn = Pattern.compile(ipReg);
    for (int i = 1; i < totalPage; i++) {
      if (getCurrentProxyNumber() >= localWantedNumber.get()) {
        return;
      }
      try {
        Document doc = Jsoup.connect(baseUrl + i + "/")
            .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .header("Accept-Encoding", "gzip, deflate, sdch")
            .header("Accept-Language", "zh-CN,zh;q=0.8,en;q=0.6")
            .header("Cache-Control", "max-age=0")
            .header("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36")
            .header("Cookie", "Hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31=1462812244; _gat=1; _ga=GA1.2.1061361785.1462812244")
            .header("Host", "www.kuaidaili.com")
            .header("Referer", "http://www.kuaidaili.com/free/outha/")
            .timeout(30 * 1000)
            .get();
        Matcher m = ipPtn.matcher(doc.text());
        while (m.find()) {
          if (getCurrentProxyNumber() >= localWantedNumber.get()) {
            break;
          }
          String[] strs = m.group().split(" ");
          if (checkProxy(strs[0], Integer.parseInt(strs[1]))) {
            System.out.println("獲取到可用代理IP\t" + strs[0] + "\t" + strs[1]);
            addProxy(strs[0], strs[1], "http");
          }
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
  private static boolean checkProxy(String ip, Integer port) {
    try {
      //http://1212.ip138.com/ic.asp 可以換成任何比較快的網(wǎng)頁(yè)
      Jsoup.connect("http://1212.ip138.com/ic.asp")
          .timeout(2 * 1000)
          .proxy(ip, port)
          .get();
      return true;
    } catch (Exception e) {
      return false;
    }
  }
  private int getCurrentProxyNumber() {
    List<ProxyInfo> proxyInfos = localProxyInfos.get();
    if (proxyInfos == null) {
      proxyInfos = new ArrayList<ProxyInfo>();
      localProxyInfos.set(proxyInfos);
      return 0;
    }
    else {
      return proxyInfos.size();
    }
  }
  private void addProxy(String ip, String port, String protocol){
    List<ProxyInfo> proxyInfos = localProxyInfos.get();
    if (proxyInfos == null) {
      proxyInfos = new ArrayList<ProxyInfo>();
      proxyInfos.add(new ProxyInfo(ip, port, protocol));
    }
    else {
      proxyInfos.add(new ProxyInfo(ip, port, protocol));
    }
  }
}
class ProxyInfo {
  private String userName = "";
  private String ip;
  private String password = "";
  private String type;
  private String port;
  private int is_internet = 1;
  public ProxyInfo(String ip, String port, String type) {
    this.ip = ip;
    this.type = type;
    this.port = port;
  }
  public String getUserName() {
    return userName;
  }
  public void setUserName(String userName) {
    this.userName = userName;
  }
  public String getIp() {
    return ip;
  }
  public void setIp(String ip) {
    this.ip = ip;
  }
  public String getPassword() {
    return password;
  }
  public void setPassword(String password) {
    this.password = password;
  }
  public String getType() {
    return type;
  }
  public void setType(String type) {
    this.type = type;
  }
  public String getPort() {
    return port;
  }
  public void setPort(String port) {
    this.port = port;
  }
  public int getIs_internet() {
    return is_internet;
  }
  public void setIs_internet(int is_internet) {
    this.is_internet = is_internet;
  }
}
class ProxyResponse {
  private String success;
  private Map<String, Object> data;
  public String getSuccess() {
    return success;
  }
  public void setSuccess(String success) {
    this.success = success;
  }
  public Map<String, Object> getData() {
    return data;
  }
  public void setData(Map<String, Object> data) {
    this.data = data;
  }
}

以上這篇java代理實(shí)現(xiàn)爬取代理IP的示例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何使用@AllArgsConstructor和final 代替 @Autowired

    如何使用@AllArgsConstructor和final 代替 @Autowired

    這篇文章主要介紹了使用@AllArgsConstructor和final 代替 @Autowired方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • Spring Boot 3.0升級(jí)指南

    Spring Boot 3.0升級(jí)指南

    這篇文章主要為大家介紹了Spring Boot 3.0升級(jí)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • Mybatis-plus如何在xml中傳入自定義的SQL語(yǔ)句

    Mybatis-plus如何在xml中傳入自定義的SQL語(yǔ)句

    這篇文章主要介紹了Mybatis-plus如何在xml中傳入自定義的SQL語(yǔ)句問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • JAVA 枚舉單例模式及源碼分析的實(shí)例詳解

    JAVA 枚舉單例模式及源碼分析的實(shí)例詳解

    這篇文章主要介紹了 JAVA 枚舉單例模式及源碼分析的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • Java技術(shù)長(zhǎng)久占居主要地位的12個(gè)原因

    Java技術(shù)長(zhǎng)久占居主要地位的12個(gè)原因

    這篇文章主要為大家詳細(xì)介紹了12個(gè)Java長(zhǎng)久占居主要地位的原因,感興趣的小伙伴們可以參考一下
    2016-07-07
  • spring?boot項(xiàng)目實(shí)戰(zhàn)之實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的連接

    spring?boot項(xiàng)目實(shí)戰(zhàn)之實(shí)現(xiàn)與數(shù)據(jù)庫(kù)的連接

    在我們?nèi)粘5拈_發(fā)過程中,肯定不可避免的會(huì)使用到數(shù)據(jù)庫(kù)以及SQL?語(yǔ)句,下面這篇文章主要給大家介紹了關(guān)于spring?boot項(xiàng)目實(shí)戰(zhàn)之實(shí)現(xiàn)與數(shù)據(jù)庫(kù)連接的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • java實(shí)現(xiàn)的新浪微博分享代碼實(shí)例

    java實(shí)現(xiàn)的新浪微博分享代碼實(shí)例

    這篇文章主要介紹了java實(shí)現(xiàn)的新浪微博分享代碼實(shí)例,是通過新浪API獲得授權(quán),然后接受客戶端請(qǐng)求的數(shù)據(jù),第三方應(yīng)用發(fā)送請(qǐng)求消息到微博,喚起微博分享界面,非常的實(shí)用,有相同需要的小伙伴可以參考下。
    2015-03-03
  • Java中的Redis是什么意思

    Java中的Redis是什么意思

    Redis是一個(gè)非常強(qiáng)大的工具,它可以用來(lái)實(shí)現(xiàn)很多有趣的應(yīng)用,還可以使用Redis來(lái)實(shí)現(xiàn)分布式鎖,這樣你就可以在多線程或多進(jìn)程的環(huán)境下同步代碼,這篇文章主要介紹了Java中的Redis是什么意思,需要的朋友可以參考下
    2023-08-08
  • Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼

    Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼

    這篇文章主要介紹了Mybatis-plus實(shí)現(xiàn)主鍵自增和自動(dòng)注入時(shí)間的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Hibernate初體驗(yàn)及簡(jiǎn)單錯(cuò)誤排除代碼詳解

    Hibernate初體驗(yàn)及簡(jiǎn)單錯(cuò)誤排除代碼詳解

    這篇文章主要介紹了Hibernate初體驗(yàn)及簡(jiǎn)單錯(cuò)誤排除代碼詳解,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02

最新評(píng)論

仪征市| 房产| 盐源县| 通榆县| 孙吴县| 龙山县| 西峡县| 巴林右旗| 灵台县| 浦江县| 城口县| 正镶白旗| 碌曲县| 无为县| 黔西县| 赣榆县| 崇州市| 巴塘县| 林周县| 多伦县| 松潘县| 美姑县| 葵青区| 中阳县| 潮州市| 金沙县| 乐都县| 昭觉县| 衡东县| 绥宁县| 宁波市| 舟山市| 卢龙县| 秀山| 交城县| 新和县| 深水埗区| 大城县| 工布江达县| 桃园市| 南乐县|