Parcourir la source

if filter()的判断在python3中返回为filter对象,即使为空,if判断也会判定为True

bobobo80 il y a 9 ans
Parent
commit
c71fa1e6aa
1 fichiers modifiés avec 8 ajouts et 5 suppressions
  1. 8 5
      Util/WebRequest.py

+ 8 - 5
Util/WebRequest.py

@@ -70,12 +70,15 @@ class WebRequest(object):
         while True:
             try:
                 html = requests.get(url, headers=headers, timeout=timeout)
-                if filter(lambda key: key in html.content, retry_flag):
-                    raise Exception
-                else:
-                    return html
+                # if filter(lambda key: key in html.content, retry_flag):
+                # 原filter语句执行if判断所有情况均为True情况,python3与python2的区别?
+                # python3中filter返回filter对象,即使为空,if会判断为True
+                # python2中filter返回list对象,为空,if判断为False
+                for f in retry_flag:
+                    if f in html.content:
+                        raise Exception
+                return html
             except Exception as e:
-                print(e)
                 retry_time -= 1
                 if retry_time <= 0:
                     return