高匿名动态IP代理获取

# -*-coding:utf-8-*-
# IP proxy
import json
import time

import requests
from threading import Lock


class IpProxy(object):
_local = Lock()
_instance = None
# 代理IP过期时间, IP有效时长为60秒, 代理IP
effective_duration = 60
expire_time_stamp = 0
proxy_ip = []

# 青果网络高匿名代理IP获取参数 api_url:https://www.qg.net/doc/api/3_117_210/1846.html
ip_proxy_host = "http://share.proxy.qg.net/get"
# 密码
pwd = "xxx"
# 公共参数,产品唯一标识。
key = "xxxx"
# 指定地区提取。如果用的是省代码,可以提取到这个省下任意市的IP 。
area = "410000"
# 排除指定地区。如果用的是省代码,则不会提取到这个省的IP 。
area_ex = ""
# 按运营商提取。
# 0: 不筛选
# 1: 电信
# 2: 移动
# 3: 联通
isp = 0
# 提取个数,默认为1。
num = 1
# 去重提取,默认为false。如果为true则不会提取到已经在使用的IP资源。
distinct = False
# 选择资源池提取。
# 1: 普通池,默认
# 2: 企业池
pool = 1

def __new__(cls, *args, **kwargs):
if not cls._instance:
with cls._local:
if not cls._instance:
cls._instance = super(IpProxy, cls).__new__(cls, *args, **kwargs)
return cls._instance

def get_proxy_ip(self):
u"""获取代理IP"""
# 如果代理IP未过期
if self.expire_time_stamp > int(time.time()):
return True, self.proxy_ip

params = {}
if self.key:
params["key"] = self.key
if self.area:
params["area"] = self.area
if self.area_ex:
params["area_ex"] = self.area_ex
if self.isp:
params["isp"] = self.isp
if self.num:
params["num"] = self.num
if self.distinct:
params["distinct"] = self.distinct
if self.pool:
params["pool"] = self.pool

retry_count = 3
cur_num = 1
while cur_num <= retry_count:
re = requests.get(self.ip_proxy_host, params=params)
print(re.text)
if str(re.status_code).startswith("20"):
data_json = json.loads(re.text)
if data_json["code"] == "SUCCESS":
for item in data_json["data"]:
self.proxy_ip.append(item["server"])
print(item["server"])
self.expire_time_stamp = int(time.time()) + 60
return True, self.proxy_ip
cur_num += 1
return False, []



if __name__ == "__main__":
success_count = 0
fail_count = 0

for item in range(1000):
ip_proxy = IpProxy()
is_true, proxy_ip_list = ip_proxy.get_proxy_ip()
print(proxy_ip_list)

# if not is_true:
# fail_count += 1
# continue
#
# user_name = ip_proxy.key
# pwd = ip_proxy.pwd
# proxy_ip = proxy_ip_list[0]
#
# # 发送请求
# url = "http://httpbin.org/get"
# proxies = {
# "http": "http://%s:%s@%s" % (user_name, pwd, proxy_ip)
# # "https": "113.229.3.172:55046"
# }
# re = requests.get(url=url, proxies=proxies)
# if str(re.status_code).startswith("20"):
# data = json.loads(re.text)
# print("success, origin:%s" % data["origin"])
# success_count += 1
# else:
# fail_count += 1

print("success_count: %s" % success_count)
print("fail_count: %s" % fail_count)
posted @ 2023-11-13 19:24  你看起来真的很好吃  阅读(10)  评论(0编辑  收藏  举报