-1

how could I get to make these three lists into one list following the same order as the output in the join? Here is my code:

from bs4 import BeautifulSoup
import requests
url = 'http://www.adorocinema.com/filmes/mais-esperados/'
r = requests.get(url)

soup = BeautifulSoup(r.content, 'html.parser')
#print(soup.prettify())

html_filme = soup.find('div', attrs={'class': 'gd-col-left'})
html_filme2 = html_filme.find_all('a', attrs={'class': 'meta-title-link'})
#print(html_series.prettify())
#print(html_series2)

filmeLista = []

for texto in html_filme2:
    filme = texto.find_all(text=True)
    filmeLista.append({'nome': filme[0]})
    #print (serieLista)

diretoresLista = []

html_diretores = html_filme.find_all('div', attrs={'class': 'meta-body-item meta-body-direction'})
for diretor in html_diretores:
    diretores = diretor.find_all(text = True)
    diretoresLista.append({'diretor':diretores[3]})
    #print(diretores)
    #print(diretoresLista)

estreiasLista = []

html_estreia = html_filme.find_all('div' ,attrs={'class' : 'meta-body-item meta-body-info'})
for estreia in html_estreia:
    estreias = estreia.find_all(text = True)
    #print(estreias)
    generos = []
    try:
        generos = estreias[7].replace("\n", "") + ','+ (estreias[7] == None and ('') or estreias[9].replace("\n", ""))
    except IndexError:
        generos = estreias[5]
    estreiasLista.append({'Estreia':estreias[1],\
                            'Genero':generos})

Method Join:

filmesLista2 = "\n".join("{} , {} , {}".format(x, y, z) for x, y, z in zip(filmeLista, diretoresLista, estreiasLista))
print(filmesLista2)

entire method output enter image description here

Thank you for your help.

  • Take a look to this answer, i believe it covers what you are looking for: https://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists – Richard Apr 07 '21 at 02:07
  • 3
    Could you boil this question down to just the three lists you have and the one list you want without all the backstory of how you got the three lists? – Samwise Apr 07 '21 at 02:07
  • 1
    Yes, without the backstory :) –  Apr 07 '21 at 02:10

1 Answers1

2

You might want to try

filmesLista2 = [{**x, **y, **z} for x, y, z in zip(filmeLista, diretoresLista, estreiasLista)]

provided that your python version is ≥ 3.5.

See How do I merge two dictionaries in a single expression (taking union of dictionaries)?.

The output is as follows:

[
{'nome': 'Godzilla vs Kong', 'diretor': 'Adam Wingard', 'Estreia': '29 de abril de 2021', 'Genero': 'Ação,Aventura'},
{'nome': 'Meu Pai', 'diretor': 'Florian Zeller', 'Estreia': '9 de abril de 2021', 'Genero': '/'},
{'nome': 'Velozes & Furiosos 9', 'diretor': 'Justin Lin', 'Estreia': '22 de julho de 2021', 'Genero': 'Ação'},
{'nome': 'Miraculous - Ladybug e Cat Noir', 'diretor': 'Thomas Astruc', 'Estreia': '2021', 'Genero': 'Aventura,Animação'},
{'nome': 'Os Croods 2: Uma Nova Era', 'diretor': 'Will Gluck', 'Estreia': '1 de julho de 2021', 'Genero': 'Aventura,Animação'},
{'nome': 'Pedro Coelho 2: O Fugitivo', 'diretor': 'Emerald Fennell', 'Estreia': 'julho 2021', 'Genero': 'Aventura,Comédia'},
{'nome': 'Bela Vingança', 'diretor': 'Cate Shortland', 'Estreia': '8 de abril de 2021', 'Genero': 'Comédia,Drama'},
{'nome': 'Viúva Negra', 'diretor': 'James Gunn', 'Estreia': '9 de julho de 2021', 'Genero': 'Ação,Aventura'},
{'nome': 'O Esquadrão Suicida', 'diretor': 'Doug Liman', 'Estreia': '5 de agosto de 2021', 'Genero': 'Aventura,Fantasia'},
{'nome': 'Mundo em Caos', 'diretor': 'Felipe Novaes', 'Estreia': '8 de abril de 2021', 'Genero': 'Ação,Ficção Científica'},
{'nome': 'Chorão: Marginal Alado', 'diretor': 'Malcolm D. Lee', 'Estreia': '8 de abril de 2021', 'Genero': '/'},
{'nome': 'Space Jam: Um Novo Legado', 'diretor': 'Simon McQuoid', 'Estreia': '15 de julho de 2021', 'Genero': 'Comédia,Família'},
{'nome': 'Mortal Kombat', 'diretor': 'John Krasinski', 'Estreia': '13 de maio de 2021', 'Genero': 'Ação'},
{'nome': 'Um Lugar Silencioso - Parte II', 'diretor': 'Michael Matthews', 'Estreia': '17 de junho de 2021', 'Genero': 'Fantasia,Terror'}
]
j1-lee
  • 13,764
  • 3
  • 14
  • 26