当前位置:首页 > 日记本 > 正文内容

python requests抓取网页简单的重试机制

zhangchap2年前 (2022-04-12)日记本178
def get_api(url,retries=3):
    try:
        r = requests.get(url, headers=headers, timeout=35)
        return r.json()
    except requests.exceptions.ConnectionError:
        if retries > 0:
            time.sleep(1)
            print('访问链接出错,重新抓取')
            return get_api(url, retries - 1)

更多参考:https://blog.csdn.net/u013055678/article/details/54290481

分享给朋友:

相关文章

Nginx+PHP,PHP如何优化配置?

具体修改FPM配置文件参数: 若你的php日志出现: WARNING: [pool www] seems busy (you may need to increase pm.sta...

python jieba分词

import jieba from jieba.analyse import tfidf words = jieba.lcut('...

python 发布文章 随机分类(choice)

from random import choice catid = choice([5,6]) #choice 函数从列表中随机提取...

python读取txt文件放到Queue队列

from queue import Queue with open('kw.txt',encoding='utf-8')&nb...

python fake_useragent 模块用法

我们每次发送requests请求时通过random从中随机获取一个随机UserAgent,两行代码即可完成UserAgent的不停更换 from fake_useragent i...

python md5生成

from hashlib import md5 md5_hash = md5(title.encode('utf-8')).hexd...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。