Javaファイルを使用して、メインメソッドを実行するだけです。依存するjarパッケージはcom.alibaba.fastjson(バージョン1.2.28)およびjsoup(バージョン1.10.2)です。
POMを使用する場合、次の2つは次のとおりです。
<依存関係> groupId> com.alibaba </groupid> <artifactid> fastjson </artifactid> <bersion> 1.2.28 </version> </dependency> <dependency> groupid> org.jsoup </groupid> <artifactid> jsoup </artifactid> <バージョン
完全なコードは次のとおりです。
パッケージ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.util.util.til. java.util.regex.matcher; import java.util.regex.pattern;/** *プロキシIPを取得するには、 * com.alibaba.fastjson.jsonobject and jsoup */public class proxycralwerunusedvpn {threadlocal <integer> localwantnumber> new stourdloce> threadlocal <list <ProxyInfo >> localProxyInfos = new threadlocal <list <proxyinfo >>(); public static void main(string [] args){proxycralwerunusedvpn proxycrawler = new proxycralwerunusedvpn; /***取得するプロキシIPの数は、需要者によって指定されるものとします。 (多すぎると、リターンが遅くなります) */ proxycrawler.startcrawler(1); } / ***外部モジュール呼び出しへの入り口にさらされます* @paramは、呼び出し元が取得すると予想されるプロキシIPの数を募集しました* / public string startcrawler(int wantesenumber){localwantednumber.set(wantesenumber); 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); / *** return data*/ proxyResponse応答= 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); RESTRESTSTRINGを返します。 } private void kuaidailicom(string baseurl、int totalpage){string ipreg = "//d {1,3 }/././/d {1,3 }/././/d {1,3 }/.///d {1,3} // d {1,6}";パターン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" " 「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) Safari/537.36 ").header(" cookie "、" hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31 = 1462812244; _gat = 1; _ga = ga1.2.1061361785.146281244 "" "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(例外e){e.printstacktrace(); }}}} private static boolean checkproxy(string ip、integerポート){try {//http://1212.ip138.com/ic.aspは、より高速なWebページjsoup.connect( "http://1212.ip138.com.com/ic.asp" .asp ".asp" .asp) ")。 。得る(); trueを返します。 } catch(例外e){falseを返します。 }} private int getCurrentProxynumber(){list <proxyInfo> proxyInfos = localproxyinfos.get(); if(proxyinfos == null){proxyInfos = new arrayList <ProxyInfo>(); localproxyinfos.set(proxyinfos); 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、ポート、プロトコル)); } else {proxyinfos.add(new ProxyInfo(IP、ポート、プロトコル)); }}} class proxyinfo {private string username = "";プライベート文字列IP;プライベート文字列password = "";プライベート文字列タイプ。プライベート文字列ポート。 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(){パスワードを返します。 } 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;プライベートマップ<文字列、オブジェクト>データ; 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 Proxy Crawling Proxy IPの上記の例は、私があなたと共有したすべてのコンテンツです。参照を提供できることを願っています。wulin.comをもっとサポートできることを願っています。