首页>代理ip知识与免费资源>正文
如何用python测试代理ip是否可用
发布日期:2018/1/8 12:19:33    阅读量:10055

网络上有很多的免费代理IP,但是有效率低的可怜,这个是为什么呢?这是因为用的人太多了,同一个IP无数人用来访问同一个网站,IP就会被封掉。那么如何验证IP是否可以用呢?这里就与大家分享一下自己的一点测试代码,希望对大家有帮助。
  # -*- coding utf-8 -*-
  import urllib,urllib2,re
  from random import choice
  from scrapy.selector import Selector
  #特稳定IP 107.151.152.21880
  
107.151.136.20280,222.124.130.348080,103.14.196.748080,107.151.142.1148054.169.238.1289999
  #agent = 49.113.101.1678090,113.78.28.2058090,219.136.31.168090,119.131.83.2278090,221.221.206.2088090,116.52.16.578090,182.109.80.1499000,59.55.59.419000,115.223.201.2069000
  agent = 107.151.136.19480
  proxy= choice(agent)
  #urllib2.ProxyHandler(http http//s/  proxy)
  #content = urllib2.urlopen(http//www.ip.cn/).read()#111.192.249.170
  handlers = urllib2.ProxyHandler(http http//s/  proxy)
  opener = urllib2.build_opener(*handlers)
  #opener = urllib2.build_opener()
  content = opener.open(urllib2.quote(http//www.ip.cn/, safe=/), timeout=30).read()
  sel = Selector(text=content, type=html)
  ip = sel.re(us*(.*)s*)
  print ip

开心代理IP平台专业提供HTTP/HTTPS代理IP,欢迎咨询QQ:2873763815。