pythonweb-scrapinggoogle-search

Python google search not returning expected output


I'm trying to do a google search using python, it's basically web scraping. I'm searching using a specific phrase, and I'm expecting a detailed output. I also would like to avoid using other web scraping modules, such as bs4 (BeautifulSoup4) or urllib.

I'm using the googlesearch-python module, but it's not working as expected:

from googlesearch import search

search("my search", advanced=True)

but I get this output:

<generator object search at 0x000001B4F198F290>

and when I try using the print() function:

from googlesearch import search

print(search("my search", advanced=True))

it still returns:

<generator object search at 0x000001B4F198F290>.

(Advanced is an argument that makes the search more detailed and return more information.)

Sometimes, I also get no output at all (meaning that the program ends immediately, as if there were 0 lines of code in it), and other times I get the generator object output. Is there a way to return a clean, detailed output with the googlesearch-python module? (This question is a follow up to my previous question about the googlesearch module: "Python google search 'advanced' argument unexpected")


Solution

  • You are getting a Python generator. Try to convert it into a List to see the actual results.

    from googlesearch import search
    
    # Convert the generator to a list and print the results
    results = list(search("my search", num=10, advanced=True))
    print(results)