|
|

楼主 |
发表于 2015-7-27 17:57:53
|
显示全部楼层
本帖最后由 小山童鞋 于 2015-7-27 17:59 编辑
- import urllib.request as ur
- import re
- import random
- def open_url(url):
- response = ur.Request(url)
- response = response.add_header('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36')
- response = ur.urlopen(url)
- return response.read()
- #def find_ip(text_html):
- # str1 = r'((?:(?:(?:[0,1]?\d?\d)|(?:2[0-4]\d)|(?:25[0-5]))\.){3}(?:[0,1]?\d?\d)|(?:2[0-4]\d)|(?:25[0-5]))'
- # return re.findall(str1,text_html)
- #def deals(ips):
- # ip_list = []
- # for each in ips:
- # if -1 != each.find('.'):
- # ip_list.append(each)
- # return ip_list
- #def get_ip():
- # url = 'http://www.xici.net.co/nn'
- # text_html = open_url(url).decode('utf-8')
- # ips = find_ip(text_html)
- # ip_list = deals(ips)
- # return ip_list
- def _random_ip():
- url = 'http://www.xici.net.co/nn'
- str1 = r'((?:(?:(?:[0,1]?\d?\d)|(?:2[0-4]\d)|(?:25[0-5]))\.){3}(?:[0,1]?\d?\d)|(?:2[0-4]\d)|(?:25[0-5]))'
- text_html = open_url(url).decode('utf-8')
- ips = re.findall(str1,text_html)
- ip_list = []
- for each in ips:
- if -1 != each.find('.'):
- ip_list.append(each)
- while True:
- yield ip_list[(random.randint(0,len(ip_list)))]
- def rand_ip():
- return next(_random_ip())
复制代码
ip.rand_ip()是我写的一个获得代理IP地址的模块,原文定义如上。
获得随机获得代理IP地址这个是没有问题的。我测试过了的。 |
|