-1

Link to program: https://replit.com/@MichaelGordon5/DetailedSearch#main.py

I've coded a program that in essence, does an advanced google search, and Im planning on adding more to it later.

It uses the google search package

The issue I'm having with it is that when I print out the search results into the console:

#userquery = search term, amt= amount of results

  for j in search(userquery, tld="com", num=amt, stop=amt, pause=0): 
      print(j) 

I am unable to save the result to a text file. I have had some experience using f.write/open/close in the past but nothing I do seems to work. Any advice?

Deepfake Cake
  • 37
  • 1
  • 5
  • 1
    Running programmatic searches against Google is against Google's terms of service. If you run this Google is likely to require captcha to search Google from your IP address. – Stephen Ostermiller Oct 21 '21 at 15:33

1 Answers1

1

Note that I haven't actually tried your code, but the code below should work. I'm not sure if j needs to be cast as a str but it can't hurt.

sidenote: fix your indentation. It hurts my eyes. 4 spaces is what should always be used...

results = []
for j in search(userquery, tld="com", num=amt, stop=amt, pause=0): 
    print(j) 
    results.append(str(j))

with open("filename_goes_here.txt", "w") as outfile:
    outfile.writelines(results)

Note that you could also keep the file open and write to it whenever you print out j...

Edo Akse
  • 4,051
  • 2
  • 10
  • 21