3

我使用谷歌安全浏览 API。所以我测试了这个简单的代码:

from safebrowsinglookup import SafebrowsinglookupClient

class TestMe:
    def testMe(self):
        self.key='my_Valid_Key_Here'        
        self.client=SafebrowsinglookupClient(self.key)
        self.response=self.client.lookup('http://www.google.com')
        print(self.response)

if __name__=="__main__":
    TM=TestMe()
    TM.testMe()

无论我测试什么网站,我总是得到这个:

{'website_I_tried','错误'}

请注意,安装此 API 后,我必须更改源代码中的一些行,因为它是用 Python 2 编写的,而我使用的是 Python 3.4.1。我该如何解决这个问题?


更新:

要了解为什么会出现上述问题,我运行以下代码:

from safebrowsinglookup import SafebrowsinglookupClient  

class TestMe:
    def testMe(self):
        self.key = 'my_key_here'        
        self.client=SafebrowsinglookupClient(self.key,debug=1)
        urls = ['http://www.google.com/','http://addonrock.ru/Debugger.js/']        
        self.results = self.client.lookup(*urls)
        print(self.results['http://www.google.com/'])

if __name__ == "__main__":
    TM=TestMe()
    TM.testMe()

现在,我收到了这条消息:

BODY:
2
http://www.google.com/
http://addonrock.ru/Debugger.js/


URL: https://sb-ssl.google.com/safebrowsing/api/lookup?client=python&apikey=ABQIAAAAAU6Oj8JFgQpt0AXtnVwBYxQYl9AeQCxMD6irIIDtWpux_GHGQQ&appver=0.1&pver=3.0
Unexpected server response

name 'urllib2' is not defined
error
error
4

1 回答 1

3

该库不支持 Python3.x。

在这种情况下,您可以让它支持 Python3(还有一个打开的 Python3 兼容性拉取请求),或者手动向“Google Safebrowsing API”发出请求。

这是一个使用示例requests

import requests

key = 'your key here'
URL = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=api&apikey={key}&appver=1.0&pver=3.0&url={url}"


def is_safe(key, url):
    response = requests.get(URL.format(key=key, url=url))
    return response.text != 'malware'


print(is_safe(key, 'http://addonrock.ru/Debugger.js/'))  # prints False
print(is_safe(key, 'http://google.com'))  # prints True

一样,但没有第三方包(使用urllib.request):

from urllib.request import urlopen


key = 'your key here'
URL = "https://sb-ssl.google.com/safebrowsing/api/lookup?client=python&apikey={key}&appver=1.0&pver=3.0&url={url}"


def is_safe(key, url):
    response = urlopen(URL.format(key=key, url=url)).read().decode("utf8")
    return response != 'malware'


print(is_safe(key, 'http://addonrock.ru/Debugger.js/'))  # prints False
print(is_safe(key, 'http://google.com'))  # prints True
于 2014-07-31T13:35:22.807 回答