Loop to append Googlesearch to multiple elements of a list

Asked

Viewed 43 times

0

Hello, What’s up, little community? I am a beginner with Python and programming in general and I would like to ask a question. Briefly describing the problem: I am using Googlesearch to return the first 3 links of each element of a list. I’m trying to do this through a loop so it covers all the elements. But apparently the program returns only the search for the first element.

The loop is returning the first search 5 times. 5 in the case is Len from the Cities list :(

Thanks for your help!

Cities = [cidade y, cidade x, cidade z, ...]

y = []
for x in range(len(Cities)):
    x = 0
    for j in search(Cities[x], stop=3, pause=2): 
        links =[]
        links = links[:] + [j]
        y.append(links)
    x = x + 1

print(y)

1 answer

0


Following your own reasoning:

from googlesearch import search

Cities = ['Londres', 'Berlim', 'Paris', 'Moscou', 'Viena']

y = []
for x in Cities:
    links = []
    for j in search(x, stop=3, pause=2):
        links.append(j)
    y.append(links)
print(y)

Improving the readability of your code:

from googlesearch import search

cidades = ['Londres', 'Berlim', 'Paris', 'Moscou', 'Viena']

saida = []
for cidade in cidades:
    links = []
    for link in search(cidade, stop=3, pause=2):
        links.append(link)
    saida.append(links)
print(saida)

Improving your algorithm a little more:

from googlesearch import search

cidades = ['Londres', 'Berlim', 'Paris', 'Moscou', 'Viena']

saida = []
for cidade in cidades:
    saida.append(list(search(cidade, stop=3, pause=2)))
print(saida)

Or simply:

from googlesearch import search
cidades = ['Londres', 'Berlim', 'Paris', 'Moscou', 'Viena']
saida = [list(search(cidade, stop=3, pause=2)) for cidade in cidades]
print(saida)

All codes above produce the following output:

[
  [
    'https://en.wikipedia.org/wiki/Londres',
    'https://pt.wikipedia.org/wiki/Londres',
    'https://pt.wikipedia.org/wiki/Cidade_de_Londres'
  ],
  [
    'https://pt.wikipedia.org/wiki/Berlim',
    'https://pt.wikipedia.org/wiki/Berlim_Ocidental',
    'https://pt.wikipedia.org/wiki/Distritos_de_Berlim'
  ],
  [
    'https://oglobo.globo.com/mundo/mesas-de-volta-as-calcadas-cafes-bares-restaurantes-reabrem-em-paris-24458503',
    'https://noticias.uol.com.br/ultimas-noticias/afp/2020/06/02/milhares-de-pessoas-protestam-em-paris-contra-a-violencia-policial-na-franca.htm',
    'https://www.em.com.br/app/noticia/internacional/2020/06/03/interna_internacional,1153282/manifestacao-contra-violencia-policial-termina-em-18-detidos-em-paris.shtml'
  ],
  [
    'https://pt.wikipedia.org/wiki/Moscou',
    'https://pt.wikipedia.org/wiki/Moscou#Hist%C3%B3ria',
    'https://pt.wikipedia.org/wiki/Moscou#Geografia'
  ],
  [
    'https://pt.wikipedia.org/wiki/Viena',
    'https://pt.wikipedia.org/wiki/Viena#Hist%C3%B3ria',
    'https://pt.wikipedia.org/wiki/Viena#Geografia'
  ]
]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.