Ik ben op zoek naar een input-bestand te nemen, lees elke regel, zoeken google met die lijn en alle zoekresultaten uit de query alleen als het resultaat is van een bepaalde website te drukken. Een eenvoudig voorbeeld om mijn punt te illustreren, als ik zoek hond die ik wil alleen de resultaten afgedrukt van wikipedia, of dat nu een resultaat of tien resultaten van wikipedia. Mijn probleem is dat ik heb steeds echt raar resultaten. Hieronder is mijn Python code die een specifieke URL Ik wil de resultaten van bevat.
mijn programma
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.
Mijn output bestand is onjuist, de manier waarop het zou moeten worden geformatteerd is
query string
http://www.
http://www.
http://www.
query string
http://www.
query string
http://www.medical...
http://www.medical...