遇見問題
? ? ? ? 噓噓,不要讓太多的人知道!!!!?
? ? ? ? 代理ip是爬蟲工資必要的消費,那么如何很好的利用各家服務商提供的免費代理IP呢?
使用方案
? ? ?講解一下,就是在爬取之前先到各家服務商哪里爬取最新的可利用代理IP,然后放在ip池里,然后再去爬取目標網站。如此以來,維護好這個ip池,就可以源源不斷的接收新的可以使用的代理ip,剩下的工作就是從網上四處尋找代理網站了。
給個demo吧
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONArray;import com.alibaba.fastjson.JSONObject;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import java.util.ArrayList;import java.util.List;/** * 使用免費代理demo * * Created by zc on 2017/8/11. */public class HttpClientUtilTest { public static void main(String[] args) throws Exception { //第一步,爬取ip代理池 List<ProxyModel> proxyModelList = spriderProxyIp(); String ip = proxyModelList.get(2).getIp(); int port = proxyModelList.get(2).getPort(); System.out.println(ip + " " + port + " " + proxyModelList.get(2).getAnony()); //添加白名單 whilteList(); //請求目標地址 reqWeb(ip, port); } /** * 請求目標地址 * * @param ip 代理Ip * @param port 端口 * @throws Exception 異常 */ private static void reqWeb(String ip, int port) throws Exception { HttpClientBuilder build = HttpClients.custom(); HttpHost proxy = new HttpHost(ip, port); CloseableHttpClient client = build.setProxy(proxy).build(); String url = "http://write.blog.csdn.net/postedit/77099632"; HttpGet request = new HttpGet(url); CloseableHttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); System.out.println(EntityUtils.toString(entity)); } /** * 添加自己外網ip到訊代理白名單 * * @throws Exception 異常 */ private static void whilteList() throws Exception { String url = "http://www.xdaili.cn/ipagent/whilteList/addIp?spiderId=dce0442efaac42618205f177c2xxxxip=xx.xx.xx.xx"; HttpGet request = new HttpGet(url); CloseableHttpClient client = HttpClients.custom().build(); CloseableHttpResponse response = client.execute(request); System.out.println(response.getStatusLine()); } /** * 爬取訊代理IP池 * * @return ip集合 * @throws Exception 異常 */ private static List<ProxyModel> spriderProxyIp() throws Exception { List<ProxyModel> proxyModelList = new ArrayList<>(); String url = "http://www.xdaili.cn/ipagent//freeip/getFreeIps?page=1&rows=10"; HttpGet request = new HttpGet(url); CloseableHttpClient client = HttpClients.custom().build(); CloseableHttpResponse response = client.execute(request); HttpEntity entity = response.getEntity(); String resTxt = EntityUtils.toString(entity); JSONObject jsonObject = JSON.parseobject(resTxt); JSONArray rows = jsonObject.getJSONArray("rows"); rows.stream().map(v -> (JSONObject) v).filter(v -> v.getString("anony").equals("高匿")).forEach(v -> { ProxyModel model = new ProxyModel(); model.setIp(v.getString("ip")); model.setPort(Integer.parseInt(v.getString("port"))); model.setResponsetime(v.getString("responsetime")); model.setAnony(v.getString("anony")); proxyModelList.add(model); }); return proxyModelList; } public static class ProxyModel { private String ip; private int port; private String responsetime; private String anony; public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } public String getResponsetime() { return responsetime; } public void setResponsetime(String responsetime) { this.responsetime = responsetime; } public String getAnony() { return anony; } public void setAnony(String anony) { this.anony = anony; } }
本文由 貴州做網站公司 整理發布,部分圖文來源于互聯網,如有侵權,請聯系我們刪除,謝謝!
網絡推廣與網站優化公司(網絡優化與推廣專家)作為數字營銷領域的核心服務提供方,其價值在于通過技術手段與策略規劃幫助企業提升線上曝光度、用戶轉化率及品牌影響力。這...
在當今數字化時代,公司網站已成為企業展示形象、傳遞信息和開展業務的重要平臺。然而,對于許多公司來說,網站建設的價格是一個關鍵考量因素。本文將圍繞“公司網站建設價...
在當今的數字化時代,企業網站已成為企業展示形象、吸引客戶和開展業務的重要平臺。然而,對于許多中小企業來說,高昂的網站建設費用可能會成為其發展的瓶頸。幸運的是,隨...
鞍山科技學院是985還是211?鞍山職業技術學院的名稱不準確,但正確的名稱是遼寧科技大學,其前身是鞍山鋼鐵學院??上н|寧科技大學既不是985也不是211。其專業圍繞鋼鐵行業設置,學生就業主要面向鋼鐵企業。位于鞍山高新技術開發區。鞍山科技學院是什么級別的大學?鞍山科技大學屬于一本。鞍山大學創建于1948年,是一所以工科為主的多科性大學。鞍山科技大學開設59個本科專業,3個一級學科博士點,13個二級學...
5G需要換卡嗎?5G不需要換卡。要想不使用5G網絡,就確實要直接更換SIM卡嗎?很多用戶未知這一疑問,對此三大運營商也能提供了很多回應,2個裝甲旅4G用戶無需更換SIM卡。為什么換了新卡顯示不了5g?手機還沒有不顯示5G網絡像是有三個原因。其一,而且當前所處的位置不在5G信號覆蓋范圍內,就算是可以使用5G手機和5G手機卡,手機仍然會顯示4G網絡其二,可能會未然后打開手機的5G網絡控制開關。其四是新...
如何網上查詢交通違章?兩種查詢1.通過手機軟件交管12123,綁定機動車駕駛證進行查詢。2.攜帶機動車行駛證直接到車管所或公安機關交通管理部門查詢。無論哪種,都可以有效發現車輛的違法行為。道路交通管理部門直接開具的現場罰單的違法行為,需要按照罰單上的處理時間進行處理,否則影響違法行為,處理時收取滯納金。攝像頭拍到的違法行為可以在年檢前處理,不受影響。...