【发布时间】:2014-07-03 11:45:41
【问题描述】:
我正在使用代码来获取号码。实现语义定位的特定短语的命中数。
def hits(word1,word2=""):
query = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%s"
if word2 == "":
results = urllib.urlopen(query % word1)
else:
results = urllib.urlopen(query % word1+" "+"AROUND(10)"+" "+word2)
json_res = json.loads(results.read())
google_hits=int(json_res['responseData']['cursor']['estimatedResultCount'])
return google_hits
但是当我给出一个包含短语的长文件时,它会在某种程度上执行但返回错误
"TypeError: 'NoneType' object has no attribute '__getitem__' "
错误是动态的,因为它有时会执行一些短语,有时不会。我认为这是我正在使用的 google API 的问题。此函数使用上述计算 SO。
def so(phrase):
num = hits(phrase,"excellent")
print num
den = hits(phrase,"poor")
print den
ratio = (num/ den+0.01)*0.6403669724770642
print ratio
sop = log(ratio)
return sop
谁有想法请帮忙!!!
【问题讨论】:
-
请发布完整的堆栈跟踪。
标签: python-2.7 typeerror