我获得了谷歌搜索的10个结果。
我的情景是:
这是我的代码:
for contentIndex in self.search_response['links']:
domain = self.search_response['links'][contentIndex]['domain']
if "wikipedia.org" in domain:
google_query = ''
google_query = self.search_response['links'][contentIndex]['content']
print "wiki link"
break
elif google_instant:
google_query = ''
google_query = google_instant
print "\n \n Instant result : " + google_instant
break
else:
google_query += self.search_response['links'][contentIndex]['content']
但这种情况会崩溃。就像第一个链接不是维基链接并且即时结果存在那么它不会包含维基链接,而是即时结果。
答案 0 :(得分:0)
您在google_instant条件下突破了循环。如果在找到维基百科链接之前满足此条件,则它将始终使用google_instant链接。你真正需要做的是继续迭代结果,最后检查是否有维基百科或谷歌即时链接。
search_results = ''
wikipedia_result = None
google_instant_result = None
for contentIndex in self.search_response['links']:
domain = self.search_response['links'][contentIndex]['domain']
if "wikipedia.org" in domain:
wikipedia_rsult = self.search_response['links'][contentIndex]['content']
print "wiki link"
elif google_instant:
google_instant_result = google_instant
print "\n \n Instant result : " + google_instant
else:
search_results += self.search_response['links'][contentIndex]['content']
google_query = wikipedia_result or google_instant or search_results