Google 从 python 应用搜索
Google search from python app
我正在尝试读取输入文件的每一行并使用该行搜索 google 并打印查询的搜索结果。我得到了来自维基百科的第一个搜索结果,这很好,但后来我收到错误:文件 "test.py",第 24 行,在
字典[str(lineToRead)].append(str(i))
键错误:'mouse'
input file pets.txt looks like this:
cat
dog
bird
mouse
inputFile = open("pets.txt", 'r') # Makes File object
outputFile = open("results.csv", "w")
dictionary = {} # Our "hash table"
compare = "https://en.wikipedia.org/wiki/" # urls will compare against this string
for line in inputFile.read().splitlines():
# ---- testing ---
print line
lineToRead = line
inputFile.close()
from googlesearch import GoogleSearch
gs = GoogleSearch(lineToRead)
#gs.results_per_page = 5
#results = gs.get_results()
for i in gs.top_urls():
print i # check to make sure this is printing out url's
compare2 = i
if compare in compare2: # compare the two url's
dictionary[str(lineToRead)].append(str(i)) #write out query string to dictionary key & append the urls
for i in dictionary:
print i
outputFile.write(str(i))
for j in dictionary[i]:
print j
outputFile.write(str(j))
#outputFile.write(str(i)) #write results for the query string to the results file.
#to check if hash works print key /n print values /n print : /n print /n
#-----------------------------------------------------------------------------
杰里米班克斯是对的。如果您在没有先为 dictionary[str(lineToRead)]
初始化值的情况下编写 dictionary[str(lineToRead)].append(str(i))
,您将得到一个错误。
您似乎还有一个错误。 lineToRead
的值将始终为 mouse
,因为您在搜索任何内容之前已经循环并关闭了输入文件。您可能想要遍历 inputFile 中的每个单词(即猫、狗、鸟、老鼠)
要解决此问题,我们可以编写以下内容(假设您希望将查询字符串列表作为每个搜索词在字典中的值):
for line in inputFile.read().splitlines(): # loop through each line in input file
lineToRead = line
dictionary[str(lineToRead)] = [] #initialize to empty list
for i in gs.top_urls():
print i # check to make sure this is printing out url's
compare2 = i
if compare in compare2: # compare the two url's
dictionary[str(lineToRead)].append(str(i)) #write out query string to dictionary key & append the urls
inputfile.close()
您可以删除为 'testing' inputFile 编写的 for 循环。
我正在尝试读取输入文件的每一行并使用该行搜索 google 并打印查询的搜索结果。我得到了来自维基百科的第一个搜索结果,这很好,但后来我收到错误:文件 "test.py",第 24 行,在 字典[str(lineToRead)].append(str(i)) 键错误:'mouse'
input file pets.txt looks like this:
cat
dog
bird
mouse
inputFile = open("pets.txt", 'r') # Makes File object
outputFile = open("results.csv", "w")
dictionary = {} # Our "hash table"
compare = "https://en.wikipedia.org/wiki/" # urls will compare against this string
for line in inputFile.read().splitlines():
# ---- testing ---
print line
lineToRead = line
inputFile.close()
from googlesearch import GoogleSearch
gs = GoogleSearch(lineToRead)
#gs.results_per_page = 5
#results = gs.get_results()
for i in gs.top_urls():
print i # check to make sure this is printing out url's
compare2 = i
if compare in compare2: # compare the two url's
dictionary[str(lineToRead)].append(str(i)) #write out query string to dictionary key & append the urls
for i in dictionary:
print i
outputFile.write(str(i))
for j in dictionary[i]:
print j
outputFile.write(str(j))
#outputFile.write(str(i)) #write results for the query string to the results file.
#to check if hash works print key /n print values /n print : /n print /n
#-----------------------------------------------------------------------------
杰里米班克斯是对的。如果您在没有先为 dictionary[str(lineToRead)]
初始化值的情况下编写 dictionary[str(lineToRead)].append(str(i))
,您将得到一个错误。
您似乎还有一个错误。 lineToRead
的值将始终为 mouse
,因为您在搜索任何内容之前已经循环并关闭了输入文件。您可能想要遍历 inputFile 中的每个单词(即猫、狗、鸟、老鼠)
要解决此问题,我们可以编写以下内容(假设您希望将查询字符串列表作为每个搜索词在字典中的值):
for line in inputFile.read().splitlines(): # loop through each line in input file
lineToRead = line
dictionary[str(lineToRead)] = [] #initialize to empty list
for i in gs.top_urls():
print i # check to make sure this is printing out url's
compare2 = i
if compare in compare2: # compare the two url's
dictionary[str(lineToRead)].append(str(i)) #write out query string to dictionary key & append the urls
inputfile.close()
您可以删除为 'testing' inputFile 编写的 for 循环。