获得代理ip并验证
来源:互联网 发布:windows日志在哪里 编辑:程序博客网 时间:2024/05/17 03:41
import reimport requestsimport threadingfrom queue import Queuefrom lxml import etreeclass ProxyIp: @classmethod def get_proxy_ip(cls): url1 = "http://www.youdaili.net/" href_queue = Queue() # 到该网站找到需要抓取ip的链接 ip_queue = Queue() # 网站上能找到的ip ip_valid = [] # 有效的代理 url1_resp = requests.get(url1) content = url1_resp.content.decode() html = etree.HTML(content) box = html.xpath("//div[@class='m_box2']/ul/li/a") for i in range(24): href_queue.put(box[i].get("href")) # 将链接放到队列里 while not href_queue.empty(): # 获取所有代理 ip_href = href_queue.get() try: resp = requests.get(ip_href) content = resp.text html = etree.HTML(content) ips = html.xpath("//div[@class='cont_font']/p/span[1]") for ip in ips[0].itertext(): ip_queue.put(ip) except Exception as e: print(e) break checkth = [] print("Starting Checking...") for i in range(30): # 使用多线程进行验证是否可用 checkth.append(CheckThreads(ip_queue, ip_valid)) for th in checkth: th.start() for th in checkth: th.join() ip_valid = list(set(ip_valid)) return ip_validclass CheckThreads(threading.Thread): mutex = threading.Lock() def __init__(self, ip_queue, ip_valid): super(CheckThreads, self).__init__() self.ip_queue = ip_queue self.ip_valid = ip_valid def run(self): while not self.ip_queue.empty(): try: ip_re = re.compile(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{2,5})') ip = self.ip_queue.get() ip_proxy = ip_re.search(ip).group() proxies = {"http": ip_proxy} headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding": "gzip, deflate", "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3", "Cache-Control": "no-cache", "Connection": "keep-alive", "Host": "www.1356789.com", "Pragma": "no-cache", "Referer": "http://m.ip138.com/", } resp = requests.get(url="http://www.1356789.com/", proxies=proxies, headers=headers, timeout=10) if resp.status_code == 200: page = resp.text ip_url = re.search(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', page).group() ip_now = re.search(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', ip_proxy).group() if ip_url == ip_now: # 判断是否是匿名代理 self.ip_valid.append(ip_proxy + " 匿名代理") print(ip_proxy + " 匿名代理") else: self.ip_valid.append(ip_proxy + " 透明代理") print(ip_proxy + " 透明代理") except Exception: print(ip_proxy + " 该代理ip无效或响应过慢!")if __name__ == "__main__": ip = ProxyIp.get_proxy_ip() f = open("ip.txt", "w") for i in ip: try: f.write(i + "\n") except Exception as e: print(e) f.close()
该爬虫抓取该站24个页面的代理ip,并且对抓取的ip进行过滤,过滤是否可用,以及是否为匿名代理。
来源:http://tor1024.com/spider/4218ZDsqRItW57236333
0 0
- 获得代理ip并验证
- 抓取代理IP并多线程验证
- Python爬取西刺国内高匿代理ip并验证
- java获得代理真实IP
- 从西刺代理爬取代理ip,并验证是否可用
- 代理IP批量验证程序
- C#验证代理Ip地址
- 【python】采集免费代理ip并验证可用性后存储到文件
- python 爬取西刺免费代理ip 并使用telnetlib.Telnet验证是否有效
- 获得客户端非代理Ip地址
- 反向代理及如何获得原始IP
- nginx反向代理tomcat获得客户端ip
- nginx代理 tomcat获得真实用户IP
- c# 快速验证代理IP是否有用
- c# 快速验证代理IP是否有用
- C# 验证代理IP的方法
- c# 快速验证代理IP是否有用
- C# 验证代理IP的方法
- Java学习笔记之输入输出流(二) 缓冲流、随机流、数组流、数据流、对象流
- 在移动硬盘里装多个linux系统
- C语言版的16进制与字符串互转函数
- 799
- reshape2揉数据
- 获得代理ip并验证
- C++构造函数和析构函数
- django 个人博客系统开发 - 图片上传
- nyoj 130 相同的雪花
- 基于FPGA的画面分割器
- C#学习笔记之string
- paper3—Algorithm1-Engine-Trigger-on-FCD—temp
- 如何成为一个C++高级程序员
- 算法性能评价