python 爬虫 批量获取代理ip的实例代码

yipeiwu_com6年前Python爬虫

实例如下所示:

import urllib.request
import os, re,sys,time
try:
 from StringIO import StringIO
except ImportError:
 from io import StringIO
loca = re.compile(r"""ion":"\D+", "ti""")
#伪装成浏览器
header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'}
class Getip():
 def __init__(self,diqu):
  self.ur ={"xicidaili国内普通代理 --1线":"http://www.xicidaili.com/nt/",
        
    "ip84国内普通代理 --2线":'http://www.ip84.com/dlpn-http/',
        
    'xicidaili国内高匿名代理 --1线':'http://www.xicidaili.com/nn/',
       
    'ip84国内高匿名代理 --2线':'http://www.ip84.com/dlgn-http/',
    
    'xicidaili国外高匿名代理 --1线':'http://www.xicidaili.com/wn/',
    
    'ip84国外高匿名代理 --2线':'http://www.ip84.com/gwgn-http/',
    'xicidaili国外普通代理 --1线':'http://www.xicidaili.com/wt/',
    'haodailiip国内混合代理 --3线':'http://www.haodailiip.com/guonei/',
    'haodailiip国外混合代理 --3线':'http://www.haodailiip.com/guoji/',
        }
  self.diqu = diqu
 
 def urlopen(self,url):
  global header
  try:
   req = urllib.request.Request(url, None, header)
   res=urllib.request.urlopen(req)
  
   return res
  except:
   pass
 def getip(self,ren):
  '''url = "http://proxy.ipcn.org/proxylist.html"#代理IP页面
  ip_proxy_re = re.compile(r"""\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,}""")# 直接匹配 xxx.xxx.xxx.xxx:xxxx'''
  
  url = self.ur[self.diqu]+str(ren)
  
  ip_proxy_re = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s*</td>\s*<td>\s*(\d{1,})\s*</td>\s*<[^\u4E00-\u9FA5]+>([\u4E00-\u9FA5]*\s*[\u4E00-\u9FA5]*\s*[\u4E00-\u9FA5]*)\s*<')
  #################################通用正则匹配的  格式 是  (IP,端口,地区) 地区有可能包含换行和空格
  try:
   data = self.urlopen(url).read().decode('utf-8')
  except:
   return None
  
  self.rel = []
  
  ip = ip_proxy_re.findall(data)
  ##########返回的IP 就是 正则匹配的结果(IP,端口,地区) 地区有可能包含换行和空格
  
  return ip
  
if __name__ == '__main__':
 g=Getip("xicidaili国内普通代理 --1线")
 import pprint
 for x in range(4):
  ips = g.getip(1)
  print('获取到ip地址一共:',len(ips))
  pprint.pprint(ips)

以上这篇python 爬虫 批量获取代理ip的实例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python2.7实现爬虫网页数据

python2.7实现爬虫网页数据

最近刚学习Python,做了个简单的爬虫,作为一个简单的demo希望帮助和我一样的初学者。 代码使用python2.7做的爬虫  抓取51job上面的职位名,公司名,薪资,发布...

Python爬虫爬取美剧网站的实现代码

Python爬虫爬取美剧网站的实现代码

一直有爱看美剧的习惯,一方面锻炼一下英语听力,一方面打发一下时间。之前是能在视频网站上面在线看的,可是自从广电总局的限制令之后,进口的美剧英剧等貌似就不在像以前一样同步更新了。但是,作为...

零基础写python爬虫之爬虫框架Scrapy安装配置

零基础写python爬虫之爬虫框架Scrapy安装配置

前面十章爬虫笔记陆陆续续记录了一些简单的Python爬虫知识, 用来解决简单的贴吧下载,绩点运算自然不在话下。 不过要想批量下载大量的内容,比如知乎的所有的问答,那便显得游刃不有余了点。...

python可视化爬虫界面之天气查询

python可视化爬虫界面之天气查询

执行效果如下: from tkinter import * import urllib.request import gzip import json from tkinter...

python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解

python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解

这篇博客是自己《数据挖掘与分析》课程讲到正则表达式爬虫的相关内容,主要简单介绍Python正则表达式爬虫,同时讲述常见的正则表达式分析方法,最后通过实例爬取作者的个人博客网站。希望这篇基...