Search code examples
pythongenerator

TypeError on Generator object with Linux Mint. Code works fine in Win11


I have code for running a google search from terminal. The weird thing is it works just fine in my win11, but when I launch it in Linux Mint I get a TypeError.

The code creates an error in line 13

Exception has occurred: TypeError
'generator' object is not subscriptable
    for i in result[:num_results]:
TypeError: 'generator' object is not subscriptable

Any ideas why this is occuring and how to fix it?

Code below:

from googlesearch import search
import webbrowser, sys
import time

searching_for = input(("Input search words: "))

num_results = int(input("How many results : ") or "3")

result = search(searching_for)

for i in result[:num_results]:
    webbrowser.open(i)
    time.sleep(1)

Solution

  • search returns a generator.

    Use:

    for i in list(result)[:num_results]: