lundi 30 mars 2020

How to extract and print the text inside all tags in a table with python

I would like to extract text (number) from inside all tags within a table in python. I am new to coding python so excuse the messiness in my code. Here is my code for this section.

r = requests.get(saurl)
soupsa = BeautifulSoup(r.text, 'html.parser')
cases_table = soupsa.find('table')
for state in cases_table.find_all('tbody'):
rows = state.find_all('tr')
for row in rows:
    numcases = row.find('class="numeric"')
    aunumcases = row.find('td class="numeric"')
    print(aunumcases)

The html table that i am trying to scrape looks like this.

<tbody>
      <tr>
        <th>
          Location
        </th>
        <th class="text--align-right">
          Confirmed cases*            </th>
      </tr>
      <tr>
        <td>
            <p>Australian Capital Territory</p>
        </td>
        <td class="numeric">
            <p><span>78</span></p>
        </td>
      </tr>
      <tr>
        <td>
          <p>New South Wales</p>
        </td>
        <td class="numeric">
          2,032            </td>
      </tr>
      <tr>
        <td>
          <p>Northern Territory</p>
        </td>
        <td class="numeric">
            14            </td>
      </tr>
      <tr>
        <td>
          <p>Queensland</p>
        </td>
        <td class="numeric">
          689            </td>
      </tr>
      <tr>
        <td>
          <p>South Australia</p>
        </td>
        <td class="numeric">
          305            </td>
      </tr>
      <tr>
        <td>
          <p>Tasmania</p>
        </td>
        <td class="numeric">
          65            </td>
      </tr>
      <tr>
        <td>
          <p>Victoria</p>
        </td>
        <td class="numeric">
          821            </td>
      </tr>
      <tr>
        <td>
          <p>Western Australia</p>
        </td>
        <td class="numeric">
          355            </td>
      </tr>
      <tr>
        <td>
          <p><strong>Total**</strong></p>
        </td>
        <td class="numeric">
          <strong>4,359</strong>
        </td>
      </tr>
    </tbody>

The problem is when i run the code and print 'aunumcases' it returns 'none'. Any help would be really apriciated!




Aucun commentaire:

Enregistrer un commentaire