【问题标题】:Google SafeBrowsing API: always getting an errorGoogle SafeBrowsing API:总是出错
【发布时间】:2014-07-31 09:07:33
【问题描述】:

我使用谷歌安全浏览 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

【问题讨论】:

  • 第二种情况下的KeyError 正在发生,因为results 是一个字典,而不是一个列表。它的键是您查找的网站,而您没有查找任何称为 0 或 1 的网站。在第一种情况下,我们绝对无法根据您提供的信息来判断发生了什么错误。将debug=1 添加到 SafebrosinglookupClient,这应该会打印出遇到的实际错误。请针对 SO 上的所有问题发布完整错误消息,包括尽可能追溯。否则我们无法为您提供帮助。
  • @user3479224 是的,谢谢,这是第一个错误,我现在会尝试修复它,但我想我仍然会以其他方式遇到问题
  • @user3479224 我更新了我的第二个短代码:我只得到error 作为输出
  • 那是因为你没有像我说的那样将debug=1 添加到 SafebrosinglookupClient 构造函数中。
  • @user3479224 对不起,我照你说的做了,你现在可以看到我的问题的完整错误。谢谢

标签: python python-3.x safe-browsing


【解决方案1】:

The library 不支持 Python3.x。

在这种情况下,您可以make it support Python3(还有一个opened pull request 表示 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

【讨论】:

  • 谢谢。但是我收到一个奇怪的错误: ImportError: no module named requests`` 即使我使用的是 Python-3.4.1 (!)
  • @Begueradj requests 是第三方,需要先安装。好的,让我只使用 stdlib 包重写它。
  • 是的,Alexander,我的评论打扰了你,而在你的回答链接上它说我必须安装它。所以我现在正在安装它,我将再次测试。问候
  • @Begueradj 很高兴为您提供帮助,我添加了仅标准库版本。
猜你喜欢
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 2013-06-12
  • 2021-11-25
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
  • 2015-12-01
相关资源
最近更新 更多