Python - Duda Función Python al devolver una lista

 
Vista:

Duda Función Python al devolver una lista

Publicado por Jesus (1 intervención) el 09/03/2020 10:08:52
Necesito ayuda respecto a esta función que he realizado. Tengo que devolver "children_col", el cual contiene todos los elementos HTML de la clase "izq" de cada uno de los enlaces sobre los que se va iterando. SIn embargo, no me devuelve nada.

P.S: children_col es una variable global que he creado al principio.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
def html_data():
        res2 = []
        res = BeautifulSoup(html.read(), "html.parser")
        children = res.findAll("td",{"class":"izq"})
        for child in children:
            res = child.find_all("a")
            for res1 in res:
                res1.attrs['href'] = "http://www.listadomanga.es/" + res1.attrs['href']
                enlaces.append(res1.attrs['href'])
                titulos.append(res1.string)
 
        for i in range(len(enlaces)):
            html2 = urlopen(enlaces[i])
            col_html = BeautifulSoup(html2.read(),"html.parser")
            res2 = col_html.findAll("td",{"class":"izq"})
            for j in res2:
                children_col.append(j)
        return children_col
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de joel
Val: 3.475
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Duda Función Python al devolver una lista

Publicado por joel (901 intervenciones) el 09/03/2020 13:13:53
Hola Jesus, como te he comentado en el chat... aquí te adjunto el código final que hemos desarrollado utilizando yield:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from urllib.request import urlopen
from bs4 import BeautifulSoup
 
def html_data():
    html="http://www.listadomanga.es/lista.php"
    res = BeautifulSoup(urlopen(html).read(), "html.parser")
    children = res.findAll("td",{"class":"izq"})
    enlaces=[]
    for child in children:
        res = child.find_all("a")
        for res1 in res:
            res1.attrs['href'] = "http://www.listadomanga.es/" + res1.attrs['href']
            enlaces.append(res1.attrs['href'])
 
    for i in enlaces:
        col_html = BeautifulSoup(urlopen(i).read(),"html.parser")
        res2 = col_html.findAll("td",{"class":"izq"})
        for j in res2:
            yield j
 
misDatos = html_data()
for i in misDatos:
    print (i)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar