soup scraping from find_next_sibling examples compile beautiful python beautifulsoup

python - scraping - ¿Cómo se obtienen todas las filas de una tabla en particular usando BeautifulSoup?



select beautifulsoup (2)

Si alguna vez tiene tablas anidadas (como en los sitios web diseñados por la vieja escuela), el enfoque anterior podría fallar.

Como solución, es posible que desee extraer primero las tablas no anidadas:

html = ''''''<table> <tr> <td>Top level table cell</td> <td> <table> <tr><td>Nested table cell</td></tr> <tr><td>...another nested cell</td></tr> </table> </td> </tr> </table>'''''' from bs4 import BeautifulSoup soup = BeautifulSoup(html, ''lxml'') non_nested_tables = [t for t in soup.find_all(''table'') if not t.find_all(''table'')]

Alternativamente, si desea extraer el contenido de todas las tablas, incluidas las que anidan otras tablas, puede extraer solo los encabezados tr y th / td nivel superior. Para esto, debe desactivar la recursión al llamar al método find_all :

soup = BeautifulSoup(html, ''lxml'') tables = soup.find_all(''table'') cnt = 0 for my_table in tables: cnt += 1 print (''=============== TABLE {} ===============''.format(cnt)) rows = my_table.find_all(''tr'', recursive=False) # <-- HERE for row in rows: cells = row.find_all([''th'', ''td''], recursive=False) # <-- HERE for cell in cells: # DO SOMETHING if cell.string: print (cell.string)

Salida:

=============== TABLE 1 =============== Top level table cell =============== TABLE 2 =============== Nested table cell ...another nested cell

Estoy aprendiendo Python y BeautifulSoup para raspar los datos de la web y leer una tabla HTML. Puedo leerlo en Open Office y dice que es la Tabla # 11.

Parece que BeautifulSoup es la opción preferida, pero ¿puede alguien decirme cómo agarrar una mesa en particular y todas las filas? He mirado la documentación del módulo, pero no puedo entenderlo. Muchos de los ejemplos que he encontrado en línea parecen hacer más de lo que necesito.


Esto debería ser bastante sencillo si tiene una porción de HTML para analizar con BeautifulSoup. La idea general es navegar a su tabla utilizando el método findChildren , luego puede obtener el valor de texto dentro de la celda con la propiedad de string .

>>> from BeautifulSoup import BeautifulSoup >>> >>> html = """ ... <html> ... <body> ... <table> ... <th><td>column 1</td><td>column 2</td></th> ... <tr><td>value 1</td><td>value 2</td></tr> ... </table> ... </body> ... </html> ... """ >>> >>> soup = BeautifulSoup(html) >>> tables = soup.findChildren(''table'') >>> >>> # This will get the first (and only) table. Your page may have more. >>> my_table = tables[0] >>> >>> # You can find children with multiple tags by passing a list of strings >>> rows = my_table.findChildren([''th'', ''tr'']) >>> >>> for row in rows: ... cells = row.findChildren(''td'') ... for cell in cells: ... value = cell.string ... print "The value in this cell is %s" % value ... The value in this cell is column 1 The value in this cell is column 2 The value in this cell is value 1 The value in this cell is value 2 >>>