谷歌搜索python应用程序

时间:2015-12-18 04:06:06

标签: python google-search-api

我尝试读取每行的输入文件并使用该行搜索google并从查询中打印搜索结果。我得到了第一个来自维基百科的搜索结果很棒,但后来我收到了错误:文件" test.py",第24行,     字典[STR(lineToRead)。追加(STR(i))的 KeyError:'鼠标'

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

#-----------------------------------------------------------------------------

0 个答案:

没有答案