无法在python中使用google搜索浏览api查找

无法在python中使用google搜索浏览api查找,python,google-api,safe-browsing,Python,Google Api,Safe Browsing,我试图在python脚本中实现Google安全浏览API,但无法使其正常工作。代码如下所示 import urllib2 key = 'mykey' 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 = urlli

我试图在python脚本中实现Google安全浏览API,但无法使其正常工作。代码如下所示

import urllib2
key = 'mykey'
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 = urllib2.urlopen(url).read().decode("utf8")
    return reponse != 'malware'

print(is_safe(key, 'http://google.com')) #This should return True
print(is_safe(key, 'http://steam.com.co.in')) # This should return False

当我运行代码时,对于这两个查询,它都返回True,这不应该是真的,因为第二个URL肯定是恶意软件。

如果您使用的是python3,请尝试此代码

from urllib.request import urlopen
key = "mykey"
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))
    return response.read().decode("utf8") != 'malware'

print(is_safe(key, "http://www.gumblar.cn/599")) #This should return False
您所犯的错误是将url传递给urlopen而不是url。此外,您没有使用.format传递url和url字符串的键 对于python 2.7

import urllib2
key = "mykey"
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 = urllib2.urlopen(URL.format(key=key, url=url))
    return response.read().decode("utf8") != 'malware'

print(is_safe(key, "http://www.gumblar.cn/599")) 

代码给了我一个错误。返回response.read.decodeutf8!='恶意软件的SyntaxError:E0L扫描字符串文字时,代码是否与python 2.7一起工作?错误现在更改为urllib2.HTTPError:HTTP错误403:禁止。。有线索吗?感谢您使用google console中的密钥或此链接中的密钥。你也在你的控制台中启用了安全浏览api。错误是因为它找不到从谷歌开发者控制台启用的urlI,在那里你必须输入IP地址。此外,我刚刚尝试了一个从链接,但仍然没有工作。注意,我在虚拟机上运行代码,而不是在我设置了所有Google开发者控制台的主机上运行代码。这就是问题所在吗?我不确定,但从这个链接来看,它对我来说很好