谷歌搜索Python程序

时间:2015-12-19 01:12:40

标签: python google-search

我正在尝试获取输入文件,读取每一行,使用该行搜索google并仅在查询结果来自特定网站时打印查询中的所有搜索结果。一个简单的例子来说明我的观点,如果我搜索狗我只想要从维基百科打印的结果,无论是一个结果还是维基百科的十个结果。我的问题是我得到了非常奇怪的结果。下面是我的Python代码,其中包含我想要的结果的特定URL。

我的节目

inputFile = open("small.txt", 'r') # Makes File object
outputFile = open("results1.txt", "w") 
dictionary = {}  # Our "hash table"
compare = "www.someurl.com/" # urls will compare against this string

from googlesearch import GoogleSearch

for line in inputFile.read().splitlines():
    lineToRead = line
    dictionary[lineToRead] = [] #initialzed to empty list
    gs = GoogleSearch(lineToRead)
    for url in gs.top_urls():
        print url # check to make sure this is printing URLs
        compare2 = url
        if compare in compare2: #compare the two URLs, if they match 
            dictionary[lineToRead].append(url) #write out query string to dictionary key & append EACH url that matches 
inputFile.close()

for i in dictionary:
    print i # this print is a test that shows what the query was in google (dictionary key)
    outputFile.write(i+"\n")
    for j in dictionary[i]: 
        print j # this print is a test that shows the results from the query which should look like correct URL: "www.medicaldepartmentstore.com/..."(dictionary value(s))
        outputFile.write(j+"\n") #write results for the query string to the output file.

我的输出文件不正确,应该格式化的方式是

query string
http://www.
http://www.
http://www.
query string
http://www.
query string
http://www.medical...
http://www.medical...

2 个答案:

答案 0 :(得分:0)

您可以在查询时将结果的范围限制在特定网站(例如维基百科)吗?例如,使用:

gs = GoogleSearch("site:wikipedia.com %s" % query) #as shown in https://pypi.python.org/pypi/googlesearch/0.7.0

这会指示Google仅返回该域中的结果,因此您无需在看到结果后对其进行过滤。

答案 1 :(得分:0)

我认为@Cahit有正确的想法。获取只是查询字符串的唯一原因是因为您要查找的域不在top_urls()中。您可以通过检查给定键的字典中包含的数组是否为空来验证这一点

for i in dictionary:
    outputFile.write("%s: " % str(i))
    if len(dictionary[i]) == 0:
        outputFile.write("No results in top_urls\n")
    else:
        outputFile.write("%s\n" % ", ".join(dictionary[i]))