(1)免费ip使用地址:www.xicidaili.com
(2)编写脚本测试ip是否有效
推荐大家看一下这个博客:https://blog.csdn.net/Oscer2016/article/details/75000148这
这篇博客很好地展示了免费ip的获取和测试是否能用的情况.
下面是我在别的博客上看到的,拿过来使用,发现很难用,有的时候还无法测试出结果.
方法一:使用requests进行测试
import requests
try:
result = requests.get('https://blog.csdn.net/tingfenyijiu/article/details/77937481', proxies={"http":"http://118.135.217.7:80"})
print result
print result.text
except:
print 'connect failed'
else:
print 'success'
测试结果返回页面信息:
/usr/bin/python2.7 /home/gdzhang/下载/workspace/FirmCrawler/FirmCrawler/test_ip_by_request.py
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:339: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
SNIMissingWarning
/usr/local/lib/python2.7/dist-packages/urllib3/util/ssl_.py:137: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
InsecurePlatformWarning
<Response [200]>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<link rel="canonical" href="https://blog.csdn.net/tingfenyijiu/article/details/77937481"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="renderer" content="webkit"/>
<meta name="force-rendering" content="webkit"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="referrer" content="always">
<meta name="description" content="验证代码Demo
# -*- coding: utf-8 -*-
import requests
def gitIp():
ipList=[]
with open("../../proxies.txt", "r") as f:
ipList=f.read().split("\n")
# print ipList
# print "---" />
<meta name="keywords" content="python,爬虫" />
<meta http-equiv="Cache-Control" content="no-siteapp" /><link rel="alternate" media="handheld" href="#" />
<meta name="shenma-site-verification" content="5a59773ab8077d4a62bf469ab966a63b_1497598848">
<script src="https://csdnimg.cn/release/phoenix/vendor/tingyun/tingyun-rum-blog.js"></script>
<link href="https://csdnimg.cn/public/favicon.ico" rel="SHORTCUT ICON">
<title>python学习之二 代理ip有效性验证 - CSDN博客</title>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/detail-bdc51faa7c.min.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/themes/skin3-template/skin3-template-9b39979775.min.css">
<script type="text/javascript">
var username = "tingfenyijiu";
var blog_address = "https://blog.csdn.net/tingfenyijiu";
var static_host = "https://csdnimg.cn/release/phoenix/";
var currentUserName = "";
var isShowAds = true;
var isOwner = false;
var loginUrl = "https://passport.csdn.net/account/login?from=https://blog.csdn.net/tingfenyijiu/article/details/77937481"
var blogUrl = "https://blog.csdn.net/";
//页面皮肤样式
var curSkin = "skin3-template";
// 第四范式所需数据
var articleTitles = "python学习之二 代理ip有效性验证";
var articleID = "77937481";
</script>
<script type="text/javascript">
// Traffic Stats of the entire Web site By baidu
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?6bcd52f51e9b3dce32bec4a3997715ac";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
// Traffic Stats of the entire Web site By baidu end
</script>
<script src="https://csdnimg.cn/public/common/libs/jquery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="https://csdnimg.cn/rabbit/exposure-click/main-1.0.6.js"></script>
<!-- 新版上报 -->
<script src="//g.csdnimg.cn/track/1.1.1/track.js" type="text/javascript"></script>
<!-- 新版上报end -->
<link rel="stylesheet" href="https://csdnimg.cn/public/sandalstrap/1.3/css/sandalstrap.min.css">
<style>
.MathJax, .MathJax_Message, .MathJax_Preview{
display: none
}
</style>
</head>
<!-- nodata 为 第四范式接口有问题时样式不变的处理 -->
<body class="nodata">
<link rel="stylesheet" href="https://csdnimg.cn/public/common/toolbar/content_toolbar_css/content_toolbar.css">
<script id="toolbar-tpl-scriptId" src="https://csdnimg.cn/public/common/toolbar/js/content_toolbar.js" type="text/javascript" domain="https://blog.csdn.net/"></script>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/blog_code-c3a0c33d5c.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/vendor/pagination/paging.css">
<script type="text/javascript" src="//static.mediav.com/js/mvf_news_feed.js"></script>
<div style="display:none;">
<img src="" onerror='setTimeout(function(){if(!/(csdn.net|iteye.com|baiducontent.com|googleusercontent.com|360webcache.com|sogoucdn.com|bingj.com|baidu.com)$/.test(window.location.hostname)){window.location.href="\x68\x74\x74\x70\x73\x3a\x2f\x2f\x77\x77\x77\x2e\x63\x73\x64\x6e\x2e\x6e\x65\x74"}},3000);'>
</div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/chart-3456820cac.css" />
<script src="https://dup.baidustatic.com/js/ds.js"></script>
<div class="container clearfix" id="mainBox">
<!-- <div id="breadcrumbs">
<span><a href="https://blog.csdn.net/">博客首页</a></span>
<i>></i>
<span><a href="https://blog.csdn.net/tingfenyijiu">袁军伟的博客...</a></span>
<i>></i>
<span>python学习之二 代理ip有效性验证</span>
</div> -->
<ul class="recommend-right">
</ul>
<main>
<div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<span class="article-type type-1 float-left">原</span> <h1 class="title-article">python学习之二 代理ip有效性验证</h1>
</div>
<div class="article-info-box">
<div class="article-bar-top">
<span class="time">2017年09月11日 20:31:25</span>
<span class="read-count">阅读数:11793</span>
<span class="tags-box artic-tag-box">
<span class="label">标签:</span>
<a data-track-click='{"mod":"popu_626","con":"python"}' data-track-view='{"mod":"popu_626","con":"python"}' class="tag-link" href="http://so.csdn.net/so/search/s.do?q=python&t=blog" target="_blank">python <a data-track-click='{"mod":"popu_626","con":"爬虫"}' data-track-view='{"mod":"popu_626","con":"爬虫"}' class="tag-link" href="http://so.csdn.net/so/search/s.do?q=爬虫&t=blog" target="_blank">爬虫 </a>
</span>
<div class="tags-box space">
<span class="label">个人分类:</span>
<a class="tag-link" href="https://blog.csdn.net/tingfenyijiu/article/category/7157134" target="_blank">爬虫 <a class="tag-link" href="https://blog.csdn.net/tingfenyijiu/article/category/7157133" target="_blank">Python </a>
</div>
</div>
<div class="operating">
</div>
</div>
</div>
</div>
<article>
<div id="article_content" class="article_content clearfix csdn-tracking-statistics" data-pid="blog" data-mod=popu_307 data-dsm = "post" >
<div class="article-copyright">
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tingfenyijiu/article/details/77937481 </div>
<div class="markdown_views">
<h5 id="python学习之二-代理ip有效性验证"><strong>python学习之二 代理ip有效性验证</strong></h5>
<blockquote>
<p>验证代码Demo</p>
</blockquote>
<pre class="prettyprint"><code class=" hljs python"><span class="hljs-comment"># -*- coding: utf-8 -*-</span>
<span class="hljs-keyword">import</span> requests
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">gitIp</span><span class="hljs-params">()</span>:</span>
ipList=[]
<span class="hljs-keyword">with</span> open(<span class="hljs-string">"../../proxies.txt"</span>, <span class="hljs-string">"r"</span>) <span class="hljs-keyword">as</span> f:
ipList=f.read().split(<span class="hljs-string">"\n"</span>)
<span class="hljs-comment"># print ipList</span>
<span class="hljs-comment"># print "----------------------"</span>
<span class="hljs-keyword">return</span> ipList
ipList=[<span class="hljs-string">'https://39.88.13.3:53281'</span>,
<span class="hljs-string">'https://182.43.203.43:4306'</span>,
<span class="hljs-string">'https://183.62.71.242:3128'</span>,
<span class="hljs-string">'https://61.160.208.222:8080'</span>,
<span class="hljs-string">'https://123.179.28.159:8080'</span>,
<span class="hljs-string">'https://58.56.90.202:53281'</span>,
<span class="hljs-string">'https://222.88.144.119:53281'</span>,
<span class="hljs-string">'https://61.158.111.142:53281'</span>,
<span class="hljs-string">'https://139.224.24.26:8888'</span>,
<span class="hljs-string">'https://124.232.148.7:3128'</span>,
<span class="hljs-string">'https://218.15.23.229:53281'</span>,
<span class="hljs-string">'https://222.34.139.52:808'</span>,
<span class="hljs-string">'https://218.18.10.11:9797'</span>,
<span class="hljs-string">'https://119.136.113.49:9797'</span>]
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">valVer</span><span class="hljs-params">(proxys)</span>:</span>
badNum = <span class="hljs-number">0</span>
goodNum = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> proxy <span class="hljs-keyword">in</span> proxys:
<span class="hljs-keyword">try</span>:
proxy_host = proxy
protocol = <span class="hljs-string">'https&