【发布时间】: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