蘑菇代理调用函数,可以申请试用,给你个url,然后直接用我写好的函数

def proxy_api_get():
#下面连接写你的会员url,我的给去掉了
    targetUrl = ""
    resp = requests.get(targetUrl)
    if resp.status_code == 200:
        try:
            b = json.loads(resp.text)
            data = b['msg']
            for proxy in data:
                proxy = "%s:%s" % (proxy['ip'], proxy['port'])
                proxies = {
                    'http': 'http://' + proxy,
                    'https': 'https://' + proxy,
                }
                test_url = 'http://www.moguproxy.com/'
                headers = {
                    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
                }
                api_test = requests.get(test_url, headers=headers, proxies=proxies)
                if api_test.status_code == 200:
                    # time.sleep(random.random() * 10)
                    # print(proxies)
                    return proxies
                else:
                    print('该代理已经失效')
                    proxies = proxy_api_get()
                    return proxies
        except TypeError:
            proxy_api_get()
    else:
        print('请求出错')

 

你可能感兴趣的:(爬虫,爬虫2)