给`requests`加一个代理的装饰器


requests加一个代理的装饰器




def add_proxy(fn):
    # 增加requests代理
    def inner(*args, **kwargs):
        from requests import Session
        from functools import partial
        s = Session()
        s.proxies = {"https":'http://代理域名:8118'}
        return partial(fn, *args, **kwargs)(s)
    return inner


1. 函数使用

@add_proxy
def foo(param, s):
    r = s.get(url)
    print(r.text)
    print(param)
    
foo("hello")


2.类对象使用


class A:
    
    def __init__(self):
        pass
    
    @add_proxy    
    def get_request(self,param,s):
        r = s.get(url)
        print(param, r)
        

A().get_request("")




    
    
    


Buy me a 肥仔水!