【Python脚本】-Python查找可用代理IP

在用Python爬虫时,我们有时会用到IP代理。无意中发现一个免费代理IP的网站:http://www.xicidaili.com/nn/。但是,发现很多IP都用不了。故用Python写了个脚本,该脚本可以把能用的代理IP检测出来。脚本如下:

#encoding=utf8
import urllib2
from bs4 import BeautifulSoup
import urllib
import socket

User_Agent = 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
header = {}
header['User-Agent'] = User_Agent

'''
获取所有代理IP地址
'''
def getProxyIp():
    proxy = []
    for i in range(1,2):
        try:
            url = 'http://www.xicidaili.com/nn/'+str(i)
            req = urllib2.Request(url,headers=header)
            res = urllib2.urlopen(req).read()
            soup = BeautifulSoup(res)
            ips = soup.findAll('tr')
            for x in range(1,len(ips)):
                ip = ips[x]
                tds = ip.findAll("td")
                ip_temp = tds[1].contents[0]+"\t"+tds[2].contents[0]
                proxy.append(ip_temp)
        except:
            continue
    return proxy

'''
验证获得的代理IP地址是否可用
'''
def validateIp(proxy):
    url = "http://ip.chinaz.com/getip.aspx"
    f = open("E:\ip.txt","w")
    socket.setdefaulttimeout(3)
    for i in range(0,len(proxy)):
        try:
            ip = proxy[i].strip().split("\t")
            proxy_host = "http://"+ip[0]+":"+ip[1]
            proxy_temp = {"http":proxy_host}
            res = urllib.urlopen(url,proxies=proxy_temp).read()
            f.write(proxy[i]+'\n')
            print proxy[i]
        except Exception,e:
            continue
    f.close()

if __name__ == '__main__':
    proxy = getProxyIp()
    validateIp(proxy)

运行成功后,打开E盘下的文件,可以看到如下可用的代理IP地址和端口:

这只是爬取的第一页的IP地址,如有需要,可以多爬取几页。同时,该网站是时时更新的,建议爬取时只爬取前几页的即可。

时间: 2024-08-29 07:35:51

【Python脚本】-Python查找可用代理IP的相关文章

Python实现爬取可用代理IP

在实现爬虫时,动态设置代理IP可以有效防止反爬虫,但对于普通爬虫初学者需要在代理网站上测试可用代理IP.由于手动测试过程相对比较繁琐,且重复无用过程故编写代码以实现动态抓取可用的代理IP.动态代理IP保存在Json文件中,以供后续具体项目爬虫使用,但所爬取的代理IP是免费IP,所以可能出现当时爬取能用,过一段时间无法使用的情况. 1) 先从西刺代理网站上爬取前10页,速度较快的IP,保存到proxies数组中,其中proxy使用的是requests.get()可直接使用字典格式 1 print(

python扫描proxy并获取可用代理ip列表

mac或linux下可以work的代码如下: # coding=utf-8 import requests import re from bs4 import BeautifulSoup as bs import Queue import threading import random import re headers_useragents = [] headers_referers = [] headers_referers.append('http://www.google.com/?q=

Python获取免费的可用代理

Python获取免费的可用代理 在使用爬虫多次爬取同一站点时,常常会被站点的ip反爬虫机制给禁掉,这时就能够通过使用代理来解决.眼下网上有非常多提供最新免费代理列表的站点.这些列表里非常多的代理主机是可用的,可是也有一些是不可用的,因此须要进一步筛选.利用Python能够非常方便地筛选出可用的代理列表. 以提供免费代理信息的站点IPCN 国家地区免费代理为例,这里给出一个爬取此站点上提供的代理信息并筛选可用代理主机的程序.主要用到requests和lxml,详细代码为: # -*- coding

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

python编写的自动获取代理IP列表的爬虫-chinaboywg-ChinaUnix博客 undefined Python多线程抓取代理服务器 | Linux运维笔记 undefined java如果模拟请求重启路由器(网络爬虫常用),还有java如何下载图片 - baidu_nod的专栏 - 博客频道 - CSDN.NET undefined [多线程数据采集]使用Jsoup抓取数据+破解屏蔽ip访问. - MiniBu - 开源中国社区 undefined 单linux服务器同时拨多条AD

练习--爬取xici可用代理IP

通过爬虫实现xici可以使用的代理IP 端口 主要代码: #!/usr/bin/env python #coding:utf8 import telnetlib from urllib import request import re class getXici(): def __init__(self): self.url = "http://www.xicidaili.com" self.header = {'User-Agent': 'Mozilla/5.0 (Windows N

python2.7爬取可用代理IP

import urllib2 import random import time import re #from lxml import etree  #第三方模块 def get_proxy(page): headers = { 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36' } r

python脚本获取服务器外部连接IP并取出非本地服务器程序有关联的IP

#!/bin/env python #-*- coding:utf-8 -*- import os import sys from subprocess import Popen def get_foriegn_ip():         l = os.popen("netstat -ant|grep ESTABLISHED|awk '{print $5}'|awk -F: '{print $1}'|sort -r|uniq -c |awk '{print $2}'|grep -v 'and'|

GDB 运行PYTHON 脚本+python 转换GDB调用栈到流程图

http://tromey.com/blog/?cat=17 http://blog.csdn.net/cnsword/article/details/16337031 http://blog.csdn.net/woohello/article/details/7326615 转换GDB调用栈到流程图 http://blog.csdn.net/HorkyChen/article/details/23307921 http://blog.csdn.net/horkychen/article/det

使用Python自动获取可用代理列表

今天闲来无事,随便写的一个从代理发布网站上提取可用代理列表的脚本.运行后,可以获取 http://cn-proxy.com/ 发布的可用代理ip和端口的列表. 运行效果如下: 源代码如下,请指教: # -*- coding: utf-8 -*- # Python:      2.7.8 # Platform:    Windows # Author:      wucl # Program:     从代理网站获取可用代理 # History:     2015.6.11 import urll