国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

網(wǎng)絡(luò)爬蟲ip代理服務(wù)器【程序樣例】

2019-11-09 19:48:06
字體:
供稿:網(wǎng)友

爬蟲有的時(shí)候會(huì)遇到被禁ip的情況,這個(gè)時(shí)候你可以找一下代理網(wǎng)站,抓取一下ip,來進(jìn)行動(dòng)態(tài)的輪詢就沒問題了,也可以用別人做好的第三方ip代理平臺(tái),比如說crawlera,crawlera是一個(gè)利用代理IP地址池來做分布式下載的第三方平臺(tái)。【具體介紹請(qǐng)看這篇博客:http://blog.csdn.net/djd1234567/article/details/51741557】

package daili;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.net.InetSocketAddress;import java.net.MalformedURLException;import java.net.PRoxy;import java.net.URL;import java.net.URLConnection;/* * author:合肥工業(yè)大學(xué) 管院學(xué)院 錢洋 *1563178220@QQ.com *博客地址:http://blog.csdn.net/qy20115549/*/public class GetHtml { public static void main(String[] args) throws UnsupportedEncodingException { //輸入代理ip,端口,及所要爬取的url gethtml("183.136.217.74",8080,"http://club.autohome.com.cn/bbs/forum-c-2533-1.html?orderby=dateline&qaType=-1"); } public static String gethtml(String ip,int port,String url) throws UnsupportedEncodingException{ URL url1 = null; try { url1 = new URL(url); } catch (MalformedURLException e1) { e1.printStackTrace(); } InetSocketAddress addr = null; //代理服務(wù)器的ip及端口 addr = new InetSocketAddress(ip, port); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http proxy InputStream in = null; try { URLConnection conn = url1.openConnection(proxy); conn.setConnectTimeout(3000); in = conn.getInputStream(); } catch (Exception e) { System.out.println("ip " + " is not aviable");//異常IP } String s = convertStreamToString(in); System.out.println(s); return s; } public static String convertStreamToString(InputStream is) throws UnsupportedEncodingException { if (is == null) return ""; BufferedReader reader = new BufferedReader(new InputStreamReader(is,"gb2312")); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "/n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }}

如下圖,便可以抓取到url對(duì)應(yīng)的html內(nèi)容。

這里寫圖片描述


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赣榆县| 岳阳县| 新津县| 凤阳县| 夏津县| 榆中县| 海口市| 太原市| 仪陇县| 祥云县| 澳门| 甘南县| 靖宇县| 江安县| 阿鲁科尔沁旗| 江山市| 新干县| 德惠市| 临夏市| 文山县| 庆元县| 东乡族自治县| 兴安县| 谷城县| 黄山市| 灵川县| 拉萨市| 江陵县| 临洮县| 通州区| 古丈县| 恩施市| 定陶县| 邹平县| 宁都县| 江阴市| 玉屏| 双峰县| 甘德县| 固安县| 瑞丽市|