package com.goubanjia.test;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.ProxyConfig;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class TestDynamicIp {
public static List ipList = new ArrayList();
public static boolean gameOver = false;
public static void main(String[] args) {
long fetchIpSeconds = 5;
int threadNum = 10;
int testTime = 3;
String order = "这里换成你的订单号,百度全网代理IP获取";
System.out.println(">>>>>>>>>>>>>>全网代理动态IP测试开始<<<<<<<<<<<<<<");
System.out.println("***************");
System.out.println("接口返回IP为国内各地区,每次最多返回10个");
System.out.println("提取IP间隔 " + fetchIpSeconds + " 秒 ");
System.out.println("开启爬虫线程 " + threadNum);
System.out.println("爬虫目标网址 http://1212.ip138.com/ic.asp");
System.out.println("测试次数 3 ");
System.out.println("***************\n");
TestDynamicIp tester = new TestDynamicIp();
new Thread(tester.new GetIP(fetchIpSeconds * 1000, testTime, order)).start();
for (int i = 0; i < threadNum; i++) {
tester.new Ip138Tester(100).start();
}
while(!gameOver){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(">>>>>>>>>>>>>>全网代理动态IP测试结束<<<<<<<<<<<<<<");
System.exit(0);
}
// 抓取IP138,检测IP
public class Ip138Tester extends Thread{
@Override
public void run() {
while(!gameOver){
webParseHtml("http://1212.ip138.com/ic.asp");
try {
Thread.sleep(sleepMs);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
long sleepMs = 200;
public Ip138Tester(long sleepMs) {
this.sleepMs = sleepMs;
}
public String webParseHtml(String parentUrl) {
String html = "";
WebClient client = new WebClient();
try {
client.getOptions().setThrowExceptionOnFailingStatusCode(false);
client.getOptions().setJavaScriptEnabled(false);
client.getOptions().setCssEnabled(false);
client.getOptions().setThrowExceptionOnScriptError(false);
client.getOptions().setTimeout(10000); // 10s超时
client.getOptions().setAppletEnabled(true);
client.getOptions().setGeolocationEnabled(true);
client.getOptions().setRedirectEnabled(true);
String ipport = getAProxy();
if (ipport != null) {
ProxyConfig proxyConfig = new ProxyConfig(ipport.split(":")[0], Integer.parseInt(ipport.split(":")[1]));
client.getOptions().setProxyConfig(proxyConfig);
}else {
System.out.print(".");
return "";
}
HtmlPage page = client.getPage(parentUrl);
html = page.asXml();
if (html.length() > 0) {
html = Jsoup.parse(html).select("center").first().text();
}
System.out.println(getName() + " 使用代理 " + ipport + "请求IP138返回:" + html);
} catch (Exception e) {
return webParseHtml(parentUrl);
} finally {
client.close();
}
return html;
}
private String getAProxy() {
if (ipList.size() > 0) {
String ip = ipList.get((int)(Math.random() * ipList.size()));
return ip ;
}
return null;
}
}
// 定时获取动态IP
public class GetIP implements Runnable{
long sleepMs = 1000;
int maxTime = 3;
String order = "";
public GetIP(long sleepMs, int maxTime, String order) {
this.sleepMs = sleepMs;
this.maxTime = maxTime;
this.order = order;
}
@Override
public void run() {
long getIpTime = 0;
int time = 1;
while(!gameOver){
if(time >= 4){
gameOver = true;
break;
}
try {
java.net.URL url = new java.net.URL("http://dynamic.goubanjia.com/dynamic/get/" + order + ".html?ttl");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(3000);
connection = (HttpURLConnection)url.openConnection();
InputStream raw = connection.getInputStream();
InputStream in = new BufferedInputStream(raw);
byte[] data = new byte[in.available()];
int bytesRead = 0;
int offset = 0;
while(offset < data.length) {
bytesRead = in.read(data, offset, data.length - offset);
if(bytesRead == -1) {
break;
}
offset += bytesRead;
}
in.close();
raw.close();
String[] res = new String(data, "UTF-8").split("\n");
List ipList = new ArrayList();
for (String ip : res) {
try {
String[] parts = ip.split(",");
if (Integer.parseInt(parts[1]) > 0) {
ipList.add(parts[0]);
}
} catch (Exception e) {
}
}
if (ipList.size() > 0) {
TestDynamicIp.ipList = ipList;
System.out.println("第" + ++getIpTime + "次获取动态IP " + ipList.size() + " 个");
time += 1;
}
} catch (Exception e) {
e.printStackTrace();
System.err.println(">>>>>>>>>>>>>>获取IP出错");
}
try {
Thread.sleep(sleepMs);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
完整项目包下载地址:http://www.goubanjia.com/download/test-dynamic-ip.zip
使用动态代理IP,完全避免了被封IP的风险,爬虫效率直接提升了3倍以上。
请填写全网代理IP订单号,填写之后才可以提取到IP哦
相关推荐
通过Java网络爬虫爬取指定代理ip网上的ip,利用了jsoup、httpclient技术实现
python爬虫的基础,主要是运用代理ip顺利爬虫,以及如何查看一个隐藏的get接口
一键建立属于自己的高匿代理IP池;爬虫再也不用担心IP地址被封了,国内高匿https、http代理获取。拥有一个属于自己的代理IP池,从此爬虫畅行无阻。
爬虫使用代理ip,使用scrapy框架。
在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在公司做的东西不能开源出来。不过呢,闲暇...
网络代理,网络爬虫刷IPJava版,最新有效刷ip方法 Java代码,包括代理ip采集,设置代理服务、获取列表,刷IP,网页解析等知识
Python爬虫代理IP池,根据自己需求数量搭建代理IP池保存到本地,调用方法返回池中随机一条可用IP信息,随用随取,十分方便
xici代理IP爬虫,自动爬取后,选择验证有效性的IP,最后保存在txt文件
反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等...
这是做爬虫的免费代理池,主要是抓取网站上的免费代理
Jsoup实现省市区的爬取,突破ip的访问限制,实现动态ip代理,爬取最新的省市区信息
Jsoup实现代理IP的爬取,突破IP的访问限制,实现动态IP代理
多线程代理IP池,一直看到有关这方面的技术,最近实现了一个。简单的来说,启动后,会一直定时的获取代理ip,并自动检测代理ip的活跃度。运用多线程的技术,在极短的时间内获取大量的ip进行筛选。架构也比较清楚,...
Python爬虫代理怎么采用HTTP协议的代理IP.docxPython爬虫代理怎么采用HTTP协议的代理IP.docxPython爬虫代理怎么采用HTTP协议的代理IP.docxPython爬虫代理怎么采用HTTP协议的代理IP.docxPython爬虫代理怎么采用HTTP...
Python代理IP定向采集爬虫的设计与实现
在使用爬虫爬取网络数据时,如果长时间对一个网站进行抓取时可能会遇到IP被封的情况,这种情况可以使用代理更换ip来突破服务器封IP的限制。 随手在百度上搜索免费代理IP,可以得到一系列的网站,这里我们通过对西刺...
爬虫代理ip获取(快代理)
python爬虫 批量下载IP地址
自建ip代理池,满足个人对数据下载的要求,对爬虫学习的很有帮助。比如基本爬虫的经典四步法有了更加清楚的认识,对xpath获取网页数据更加熟练,对保存到mysql数据库(增删改查,去重),也可以保存到本地并读取数据...