Q
Q
quintbrut2020-03-19 01:03:35
Python
quintbrut, 2020-03-19 01:03:35

How to parse class content from bs4 in python?

In general, there

Class
<div class="fight-stats _enemy">
                <table width="100%" border="0" cellpadding="0" cellspacing="0" class="fight-stats-table">
                    <tbody>
                    <tr valign="top">
                        <td width="33%" align="center" valign="top">
                            <div class="fight-stat" id="enemy-stat-strength">5</div>
                            <div class="fight-stat-icon strength"></div>
                        </td>
                        <td width="33%" align="center" valign="top">
                            <div class="fight-stat" id="enemy-stat-dexterity">1</div>
                            <div class="fight-stat-icon dexterity"></div>
                        </td>
                        <td width="33%" align="center" valign="top">
                            <div class="fight-stat" id="enemy-stat-critical">10</div>
                            <div class="fight-stat-icon critical"></div>
                        </td>
                    </tr>
                    </tbody>
                </table>
            </div>

I only need to parse 3 values
5e7299fc58b7f833819187.png

​​AND put them into 3 variables: es, ed, ec.
So that in the end it would be:
es = 5
ed = 1
ec = 10


I tried here
So
info = s.get('https://example/index.php?r=fights/choose&force=1&club=official', headers=headers)
  soup = bs(info.content, "lxml")
  es = soup.find(attrs={'class': 'fight-stat'})['enemy-stat-strength']
  print("strength:", es)

but I can not understand how to write it correctly and nothing happened.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-03-19
@quintbrut

Since I don't see all the html, I can suggest the following solution:

es = soup.find('div',id='enemy-stat-strength').text
ed = soup.find('div',id='enemy-stat-dexterity').text
ec = soup.find('div',id='enemy-stat-critical').text

print('strength:',es)
print('dexterity:',ed)
print('critical:',ec)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question