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

yipeiwu_com5年前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设计】。

相关文章

Python实现爬虫设置代理IP和伪装成浏览器的方法分享

1.python爬虫浏览器伪装 #导入urllib.request模块 import urllib.request #设置请求头 headers=("User-Agent","Moz...

Python爬虫使用脚本登录Github并查看信息

Python爬虫使用脚本登录Github并查看信息

前言分析目标网站的登录方式 目标地址: https://github.com/login    登录方式做出分析: 第一,用form表单方式提交信息, 第二...

python抓取网站的图片并下载到本地的方法

实例如下所示: #!/usr/bin/python # -*- coding: UTF-8 -*- import re import urllib,urllib2; #通过url获取...

python抓取网页中链接的静态图片

本文实例为大家分享了python抓取网页中链接的静态图片的具体代码,供大家参考,具体内容如下 # -*- coding:utf-8 -*- #http://tieba.baid...

python协程gevent案例 爬取斗鱼图片过程解析

python协程gevent案例 爬取斗鱼图片过程解析

分析 分析网站寻找需要的网址 用谷歌浏览器摁F12打开开发者工具,然后打开斗鱼颜值分类的页面,如图: 在里面的请求中,最后发现它是以ajax加载的数据,数据格式为json,如图: 圈...