如果爬虫超时了...

  • 可能会用try ... except ... else。。嗯有点low。 换一种

example:

from functools import wraps
import requests


def trying(counts):
    """
     一个装饰器
     传入重试次数
     :return:
    """

    def request_dec(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            # 使用外部变量
            nonlocal counts
            while counts > 0:
                try:
                    result = func(*args, **kwargs)
                except:
                    counts -= 1
                    continue
                else:
                    return result

        return wrapper

    return request_dec


@trying(5)
def spider():
    # 由于反爬原因,有可能请求一次不成功
    rep = requests.get('https://www.xxxx.com/')
    content = rep.content


if __name__ == '__main__':
    spider()

over

版权声明:如无特殊说明,文章均为本站原创,转载请注明出处

本文链接:http://kkxl95.cn/article/1631534747/