如何通过Curl命令来测试爬虫ip

在使用爬虫ip做大数据抓取之前,正常的程序员都会先测试下爬虫ip是否正常可用,正常的方法就是通过浏览器设置爬虫ip打开测速网站是否正常,或者是通过第三方爬虫ip检测工具,又或者是自己写代码程序测试等,其实还有一种方法是通过Curl命令来测试爬虫ip是否可用。

测试socks5爬虫ip的命令:

curl --socks5 168.168.168.168:66666 https://www.baidu.com/

测试http爬虫ip的命令:

curl --connect-timeout 2 -x 168.168.168.168:88888 https://www.baidu.com/

在这里插入图片描述

注意点一:HTTP爬虫ip和socks5爬虫ip的命令是不同的,多数的程序员测试socks5爬虫ip,最后却写成了测试HTTP爬虫ip的命令,结果肯定行不通。

注意点二:在测试前应该明确要知道自己的爬虫ip是那种授权模式,我们知道爬虫ip支持两种授权模式,分别是“终端IP授权”和“用户名+密码”授权,如果是“终端IP授权”,请确保已经绑定了使用的终端的上网IP;如果是“用户名+密码”授权,在curl的时候也需要设置代理的用户名和密码。

curl做爬虫用爬虫ip

有时候会ip会封锁,所以会用一些模拟代理ip进行抓取测试。

function curl_string ($url,$user_agent,$proxy){

       $ch = curl_init();
       curl_setopt ($ch, CURLOPT_PROXY, $proxy);
       curl_setopt ($ch, CURLOPT_URL, $url);
       curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
       curl_setopt ($ch, CURLOPT_COOKIEJAR, "c:\cookie.txt");
       curl_setopt ($ch, CURLOPT_HEADER, 1);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
       curl_setopt ($ch, CURLOPT_TIMEOUT, 120);
       $result = curl_exec ($ch);
       curl_close($ch);
       return $result;

}

$url_page = "http://jshk.com.cn";
$user_agent = "Mozilla/4.0";
$proxy = "http://192.11.222.124:8000";
$string = curl_string($url_page,$user_agent,$proxy);
echo $string;

或者

$proxy = "80.25.198.25";
$proxyport = "8080";
$ch = curl_init("http://sfbay.craigslist.org/");

curl_setopt($ch, curlOPT_RETURNTRANSFER,1);
curl_setopt($ch,curlOPT_proxy,$proxy);
curl_setopt($ch,curlOPT_proxyPORT,$proxyport);
curl_setopt ($ch, CURLOPT_TIMEOUT, 120);

$result = curl_exec($ch);
echo $result;

curl_close($ch);

抓取使用

<?php
$requestUrl = 'ip138.com';
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $requestUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
curl_setopt($ch, CURLOPT_PROXY, "112.65.219.72"); //代理服务器地址
curl_setopt($ch, CURLOPT_PROXYPORT, 80); //代理服务器端口
//curl_setopt($ch, CURLOPT_PROXYUSERPWD, ":"); //http代理认证帐号,username:password的格式
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值