37,719
社区成员
发帖
与我相关
我的任务
分享
# _*_ coding:utf-8 _*_
import json
import urllib.request
import random,requests,time,re
proxyAPI = "https://proxy.horocn.com/api/v2/proxies?order_id=3JVA1648273480671756&num=1&format=json&line_separator=win&can_repeat=yes"
MaxProxyCounts = 5
ProxyCount = 0
ProxyList = ['0.0.0.0','0.0.0.0','0.0.0.0','0.0.0.0','0.0.0.0']
proxyUrl = "http://api3.xiguadaili.com/ip/?tid=558658608095386&num=1&delay=1&filter=on"
wandou = "http://api.wandoudl.com/api/ip?app_key=d0194613527bdce17fa055829abd45fd&pack=0&num=1&xy=1&type=1&lb=\r\n&mr=2&"
def TestProxy(host):
head = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
url = 'https://www.nike.com'
# proxies是requests中的代理 choice是随机使用一个IP 这里http 和 https最好都写上
try:
request = requests.get(url, proxies={'http': host,'https':host}, headers=head,timeout=3)
except Exception:
return False
return True
def getProxy():
global ProxyCount
global ProxyList
global MaxProxyCounts
if ProxyCount == 0:
r = requests.get(proxyAPI).content
json_str = json.loads(r)
for h in json_str:
try:
host = 'http://'+h['host'] + ':' +h['port']
except Exception:
continue
if TestProxy(host):
ProxyList[ProxyCount] = host
ProxyCount += 1
else:
continue
if ProxyCount == 0:
return getProxy()
ProxyCount -= 1
print(ProxyList[ProxyCount])
return ProxyList[ProxyCount]
def getSingleProxy():
host = "http://"+requests.get(proxyUrl).content
if not TestProxy(host):
return getSingleProxy()
else:
return host
def getWandou():
host = requests.get(wandou).content
print(host)
# return host
if not TestProxy(host):
print("Fail:" + host)
return getWandou()
else:
return host
getProxy()
def getProxy():
global ProxyCount
global ProxyList
global MaxProxyCounts
if ProxyCount == 0:
r = requests.get(proxyAPI).content
json_str = json.loads(r)
for h in json_str:
try:
host = 'http://'+h['host'] + ':' +h['port']
except Exception:
continue
if TestProxy(host):
ProxyList[ProxyCount] = host
ProxyCount += 1
else:
continue
if ProxyCount == 0:
return getProxy()
上面标记那里可能出问题了,你把异常的那个标记上看看,
except Exception as e:
print('异常是>>>',e)
ps管理员:我没有灌水,认真脸.