Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parsing table
#6
That should be okay to parse.
from bs4 import BeautifulSoup

html_data = '''\
<table class='Foo'>
  <thead>...</thead>
  <tbody>
    <tr>1</tr>
    <tr>2</tr>
    <tr>4</tr>
    <tr>5</tr>
    <tr>6</tr>
  </tbody>
</table>'''

soup = BeautifulSoup(html_data, 'lxml')
Test:
>>> table = soup.find('table')
>>> tbody = table.find('tbody')
>>> tbody
<tbody>
<tr>1</tr>
<tr>2</tr>
<tr>4</tr>
<tr>5</tr>
<tr>6</tr>
</tbody>

>>> for item in tbody.find_all('tr'):
...     print(item.text)  
1
2
4
5
6

>>> # CSS selector
>>> soup.select('tr')
[<tr>1</tr>, <tr>2</tr>, <tr>4</tr>, <tr>5</tr>, <tr>6</tr>]
>>> [int(i.text) for i in soup.select('tr')]
[1, 2, 4, 5, 6]
Reply


Messages In This Thread
parsing table - by ian - Apr-25-2018, 08:16 PM
RE: parsing table - by nilamo - Apr-25-2018, 08:21 PM
RE: parsing table - by ian - Apr-25-2018, 09:35 PM
RE: parsing table - by nilamo - Apr-26-2018, 02:57 AM
RE: parsing table - by ian - Apr-26-2018, 03:19 AM
RE: parsing table - by snippsat - Apr-26-2018, 05:19 AM
RE: parsing table - by ian - Apr-26-2018, 05:28 PM
RE: parsing table - by snippsat - Apr-26-2018, 08:05 PM
RE: parsing table - by nilamo - Apr-27-2018, 06:44 AM
RE: parsing table - by ian - Apr-27-2018, 12:16 PM
RE: parsing table - by snippsat - Apr-27-2018, 01:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help: Beautiful Soup - Parsing HTML table ironfelix717 2 2,703 Oct-01-2020, 02:19 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020