Jeg prøver å ta en input-fil, lese hver linje, søk google med den linjen og skrive ut alle søkeresultatene fra spørringen bare hvis resultatet er fra et bestemt nettsted. Et enkelt eksempel for å illustrere mitt poeng, hvis jeg søker dog jeg bare vil ha resultater som skrives ut fra wikipedia, enten det være ett resultat eller ti resultater fra wikipedia. Mitt problem er at jeg har fått veldig rare resultater. Nedenfor er min Python-kode som inneholder en bestemt nettadresse jeg vil ha resultater fra.
mitt program
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.
Min utdatafilen er feil, er det slik det er ment å være formatert
query string
http://www.
http://www.
http://www.
query string
http://www.
query string
http://www.medical...
http://www.medical...