Python中requests模块对代理IP的有效性验证
#!/usr/bin/env python
# coding=utf-8
# 代理IP验证有效性
# https://www.xicidaili.com/nn/
# https://www.kuaidaili.com/free/inha/1/
import urllib.request
import threading
def isActiveProxy():
url = "http://quote.stockstar.com/stock" #打算抓取内容的网页
proxy_ip=[{'http': 'http://111.177.169.24:9999'},{'http': 'http://111.177.169.24:9999'},{'http':'111.177.169.24:9999'},{}] #想验证的代理IP,HTTP要和代理IP网站一致大写
# 这么写也可以
# proxy_ip=[{'http': '111.177.169.24:9999'},{'http': '111.177.169.24:9999'}] #想验证的代理IP,HTTP要和代理IP网站一致大写
length = len(proxy_ip)
for i in range(length):
proxy_support = urllib.request.ProxyHandler(proxy_ip[i])
opener = urllib.request.build_opener(proxy_support)
# 可忽略
opener.addheaders=[("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64)")]
# 如果这么写,就是将opener应用到全局,之后所有的,
# 不管是opener.open()还是urlopen() 发送请求,都将使用自定义代理。
urllib.request.install_opener(opener)
try:
print("\r验证代理IP数据进度:{:.2f}%".format((i + 1) * 100 / length), end="")
request = urllib.request.Request(url)
# response = opener.open(request)
response = urllib.request.urlopen(request)
print("\ncode:",response.code)# 返回200代表有效
# print(response.read())
except Exception as e:
print(e)
if __name__ == "__main__":
#多线程验证
threads=[]
thread=threading.Thread(target=isActiveProxy,args=[])
thread.setDaemon(False)
thread.start()
threads.append(thread)
#阻塞主进程,等待所有子线程结束
for thread in threads:
thread.join()
# 另一种简单方法:
import requests
# proxy_ip={'http': 'http://111.177.169.24:9999'} #想验证的代理IP,HTTP要和代理IP网站一致大写
proxy_ip={'http': '111.177.169.24:9999'} #想验证的代理IP,HTTP要和代理IP网站一致大写
# 这么写也可以
# proxy_ip={'http': '111.177.169.24:9999'} #想验证的代理IP,HTTP要和代理IP网站一致大写
try:
response = requests.get('http://quote.stockstar.com/stock', proxies=proxy_ip,timeout=3)
print(response.status_code) # 成功返回:200
except Exception as e:
print('connect failed:',e)
else:
print('success:',response.status_code)