如何防止try和except块重复?

时间:2019-11-21 09:48:59

标签: python

我有一个URL列表,其中包含JSON文件。

JSON文件的存储方式不同,因此我需要try和except块来介绍不同的存储方法。 问题在于这种方法会导致某些重复,因为某些链接在不同的块中被请求两次或更多次。

我的代码:

for line in urls:
    try:
        response = requests.get(line)
        textinhalt = response.text
        #textinhalt = textinhalt.split("__IR_CURRPAGE_DATA_JSON__")[1]
        daten = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])
        r_urls.append(daten)
    except:
        pass
    try:
        response = requests.get(line)
        textinhalt = response.text
        #textinhalt = textinhalt.split("__IR_CURRPAGE_DATA_JSON__")[1]
        daten0 = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])
        r_urls.append(daten0)
    except:
        pass
    try:
        response = requests.get(line)
        textinhalt = response.text
        textinhalt = textinhalt.split("__IR_CURRPAGE_DATA_JSON__")[1]
        daten0 = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])
        r_urls.append(daten0)
    except:
        pass

如果在上一个块中成功请求了链接,是否可以以忽略链接的方式编写try / except块?

2 个答案:

答案 0 :(得分:3)

前2个try/except块被明确复制,重复它们没有功能上的好处。

相反,请仔细考虑 2 个连续阶段:

  • 提取远程资源
  • 解析JSON字符串并存储结果

因此,当 extracting 阶段失败-没有前进的方向,如果第一个 parsing 阶段失败-请尝试另一种解析:

for line in urls:
    try:
        response = requests.get(line)
        textinhalt = response.text
    except:
        continue
    try:
        try:
            daten = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])
        except:
            textinhalt = textinhalt.split("__IR_CURRPAGE_DATA_JSON__")[1]
            daten = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])

        r_urls.append(daten)
    except:
        pass

答案 1 :(得分:2)

这应该为您解决

for line in urls:
    try:
        response = requests.get(line)
        textinhalt = response.text
        #textinhalt = textinhalt.split("__IR_CURRPAGE_DATA_JSON__")[1]
        daten = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])
        r_urls.append(daten)
        continue
    except:
        pass

    try:
        response = requests.get(line)
        textinhalt = response.text
        #textinhalt = textinhalt.split("__IR_CURRPAGE_DATA_JSON__")[1]
        daten0 = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])
        r_urls.append(daten0)
        continue
    except:
        pass

    try:
        response = requests.get(line)
        textinhalt = response.text
        textinhalt = textinhalt.split("__IR_CURRPAGE_DATA_JSON__")[1]
        daten0 = json.loads(textinhalt[textinhalt.find("{"):textinhalt.rfind("}")+1])
        r_urls.append(daten0)
    except:
        pass