Просмотр исходного кода

Recoding for py3

recoding to use in python3
newlyedward 9 лет назад
Родитель
Сommit
88cfa62ad4
7 измененных файлов с 34 добавлено и 23 удалено
  1. 2 2
      DB/DbClient.py
  2. 7 6
      DB/RedisClient.py
  3. 1 1
      Manager/ProxyManager.py
  4. 10 5
      ProxyGetter/getFreeProxy.py
  5. 5 5
      Util/GetConfig.py
  6. 5 1
      Util/utilClass.py
  7. 4 3
      Util/utilFunction.py

+ 2 - 2
DB/DbClient.py

@@ -93,7 +93,7 @@ class DbClient(object):
 
 if __name__ == "__main__":
     account = DbClient()
-    print account.get()
+    print(account.get())
     account.changeTable('use')
     account.put('ac')
-    print(account)
+    print(account.get())

+ 7 - 6
DB/RedisClient.py

@@ -40,7 +40,7 @@ class RedisClient(object):
         :param value:
         :return:
         """
-        value = json.dump(value, ensure_ascii=False).encode('utf-8') if isinstance(value, (dict, list)) else value
+        value = json.dumps(value) if isinstance(value, (dict, list)) else value
         return self.__conn.sadd(self.name, value)
 
     def pop(self):
@@ -73,14 +73,15 @@ if __name__ == '__main__':
     # redis_con.put('abc')
     # redis_con.put('123')
     # redis_con.put('123.115.235.221:8800')
-    # print redis_con.getAll()
+    # redis_con.put(['123', '115', '235.221:8800'])
+    # print(redis_con.getAll())
     # redis_con.delete('abc')
-    # print redis_con.getAll()
+    # print(redis_con.getAll())
     # redis_con.pop()
-    # print redis_con.getAll()
+    # print(redis_con.getAll())
     redis_con.changeTable('raw_proxy')
 
     redis_con.put('132.112.43.221:8888')
     # redis_con.changeTable('proxy')
-    print redis_con.get_status()
-    print redis_con.getAll()
+    print(redis_con.get_status())
+    print(redis_con.getAll())

+ 1 - 1
Manager/ProxyManager.py

@@ -82,4 +82,4 @@ class ProxyManager(object):
 if __name__ == '__main__':
     pp = ProxyManager()
     # pp.refresh()
-    print pp.get_status()
+    print(pp.get_status())

+ 10 - 5
ProxyGetter/getFreeProxy.py

@@ -12,12 +12,17 @@
 -------------------------------------------------
 """
 import re
-import sys
-
 import requests
 
-reload(sys)
-sys.setdefaultencoding('utf-8')
+try:
+    from importlib import reload   #py3 实际不会实用,只是为了不显示语法错误
+except:
+    import sys     # py2
+    reload(sys)
+    sys.setdefaultencoding('utf-8')
+
+
+
 
 from Util.utilFunction import robustCrawl, getHtmlTree
 
@@ -137,4 +142,4 @@ if __name__ == '__main__':
     #     print e
 
     for e in gg.freeProxyFifth():
-        print e
+        print(e)

+ 5 - 5
Util/GetConfig.py

@@ -52,8 +52,8 @@ class GetConfig(object):
 
 if __name__ == '__main__':
     gg = GetConfig()
-    print gg.db_type
-    print gg.db_name
-    print gg.db_host
-    print gg.db_port
-    print gg.proxy_getter_functions
+    print(gg.db_type)
+    print(gg.db_name)
+    print(gg.db_host)
+    print(gg.db_port)
+    print(gg.proxy_getter_functions)

+ 5 - 1
Util/utilClass.py

@@ -33,7 +33,10 @@ class LazyProperty(object):
             return value
 
 
-from ConfigParser import ConfigParser
+try:
+    from configparser import ConfigParser # py3
+except:
+    from configparser import ConfigParser # py2
 
 
 class ConfigParse(ConfigParser):
@@ -44,6 +47,7 @@ class ConfigParse(ConfigParser):
     def __init__(self):
         ConfigParser.__init__(self)
 
+
     def optionxform(self, optionstr):
         return optionstr
 

+ 4 - 3
Util/utilFunction.py

@@ -20,8 +20,8 @@ def robustCrawl(func):
         try:
             return func(*args, **kwargs)
         except Exception as e:
-            print u"sorry, 抓取出错。错误原因:"
-            print e
+            print(u"sorry, 抓取出错。错误原因:")
+            print(e)
 
     return decorate
 
@@ -71,5 +71,6 @@ def validUsefulProxy(proxy):
         r = requests.get('https://www.baidu.com/', proxies=proxies, timeout=30, verify=False)
         if r.status_code == 200:
             return True
-    except Exception, e:
+    except Exception as e:
+        print(e)
         return False