# coding: utf-8

 import urllib2
import re
import time def getDL(page):
url = 'http://www.xicidaili.com/nt/{}'.format(page)
header = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
} req=urllib2.Request(url, headers=header)
res=urllib2.urlopen(req)
html=res.read() srclist=re.findall(r'<tr class=(.|\n)*?<td>(\d+\.\d+\.\d+\.\d+)</td>(.|\n)*?<td>(\d+)</td>(.|\n)*?<td>(HTTP|HTTPS)</td>', html)
xlist = []
for item in srclist:
xlist.append((item[5],item[1],item[3]))
return xlist def testDL(ipstr):
proxy= urllib2.ProxyHandler({'http':"{}:{}".format(ipstr[1], ipstr[2])})
opener=urllib2.build_opener(proxy)
urllib2.install_opener(opener) try:
testUrl = 'http://httpbin.org/ip'
testUrl = 'http://2017.ip138.com/ic.asp'
req=urllib2.Request(testUrl)
res=urllib2.urlopen(req).read()
print "********************* √ {} -- {}".format(ipstr, res) with open("ok.txt","a") as f:
f.write("{} {} {}\n".format(ipstr[0], ipstr[1], ipstr[2]))
f.close()
except Exception as e:
print "******** ×, {} -- {}".format(ipstr, e)
time.sleep(1) def startTask():
for page in xrange(5):
list=getDL(page+1)
for item in list:
testDL(item) if __name__ == '__main__':
startTask()

python获取代理IP并测试是否可用的更多相关文章

  1. python获取代理IP

    利用requests库获取代理,用Beautiful库解析网页筛选ip # -*- coding: utf- -*- import requests from bs4 import Beautiful ...

  2. Python学习笔记六(免费获取代理IP)

    为获取网上免费代理IP,闲的无聊,整合了一下,免费从三个代理网站获取免费代理IP,目的是在某一代理网站被限制时,仍可从可以访问的其他网站上获取代理IP.亲测可用哦!^_^  仅供大家参考,以下脚本可添 ...

  3. python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客

    python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如 ...

  4. python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性)

    python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_usera ...

  5. 分享一个获取代理ip的python函数

    分享一个获取代理ip的python函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #coding:utf-8 from bs4 import Beaut ...

  6. python检验代理ip是否可用、代理ip检验

    python检验代理ip是否可用.代理ip检验 安装相关模块: pip install requests 验证代理IP是否可用脚本: import random import telnetlib im ...

  7. Python3.x:获取代理ip以及使用

    Python3.x:获取代理ip以及使用 python爬虫浏览器伪装 #导入urllib.request模块 import urllib.request #设置请求头 headers=("U ...

  8. python获取公网ip,本地ip及所在国家城市等相关信息收藏

    python获取公网ip的几种方式       from urllib2 import urlopen   my_ip = urlopen('http://ip.42.pl/raw').read() ...

  9. python获取公网ip的几种方式

    python获取公网ip的几种方式 转 https://blog.csdn.net/conquerwave/article/details/77666226 from urllib2 import u ...

随机推荐

  1. asp.net 后台执行js

    1. 用Response.Write方法 代码如下: Response.Write("<script type='text/javascript'>alert("XXX ...

  2. JS之计时器

    JavaScript 计时事件 通过使用 JavaScript,我们有能力作到在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行.我们称之为计时事件. 在 JavaScritp 中使用计 ...

  3. 海康抓拍机SDK开发

    前言 项目采购的海康威视的抓拍机,需要首先获取抓拍图片,之后基于抓拍图片进行图像处理.本文基于海康SDK开发文档,按照开发步骤,咨询海康技术人员,现将开发过程记录如下. 主要过程的c++代码: /** ...

  4. directive例子1

    (function() { 'use strict'; angular.module('app.widgets') .directive('confirm', ['confirm2', 'toastr ...

  5. Gym.102006:Syrian Collegiate Programming Contest(寒假自训第11场)

    学习了“叙利亚”这个单词:比较温和的一场:几何的板子eps太小了,坑了几发. A .Hello SCPC 2018! 题意:给定一个排列,问它是否满足,前面4个是有序的,而且前面4个比后面的都小. 思 ...

  6. maven 构建 war文件&&Glassfish运行+部署war文件+访问(命令行模式)

    Glassfish常用命令 asadmin  start-domain  --verbose                 #启动Glassfish服务器(默认domain1) ,并在终端显示相关信 ...

  7. Light OJ 1296:Again Stone Game(SG函数打表找规律)

    Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains ...

  8. linux cent os 6 的安装

    目前,只有图片,没有仔细写,这是在虚拟机内的安装:

  9. rest-framework之响应器(渲染器)

    rest-framework之响应器(渲染器) 本文目录 一 作用 二 内置渲染器 三 局部使用 四 全局使用 五 自定义显示模版 回到目录 一 作用 根据 用户请求URL 或 用户可接受的类型,筛选 ...

  10. 《DSP using MATLAB》Problem 6.8

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...