Issue
How can I extract a particular data (ie, 39.74% in this case) followed by "Proj. EPS Growth (F1)" in the following HTML example with BeautifulSoup? I'm completely new to Python. Thank you!
<div class="high_low_table" id="high_low_table">
</table>
<tbody>
<tr>
<th class="alpha" scope="row">Proj. EPS Growth (Q1) </th>
<td>19.56%</td>
</tr>
<tr>
<th class="alpha" scope="row">Proj. EPS Growth (F1) </th>
<td>39.74%</td>
</tr>
</tbody>
</table>
</div>
from bs4 import BeautifulSoup
import requests
page = requests.get(url)
soup = BeautifulSoup(page.content, 'lxml')
data = soup.find('div', class_="high_low_table").text
Solution
In this particular case you have two th elements with identical attributes. Therefore you need to inspect the text part of those elements to identify the one you're interested in. Try this:-
from bs4 import BeautifulSoup as BS
HTML = '''
<html>
<div class="high_low_table" id="high_low_table">
<table>
<tbody>
<tr>
<th class="alpha" scope="row">Proj. EPS Growth (Q1) </th>
<td>19.56%</td>
</tr>
<tr>
<th class="alpha" scope="row">Proj. EPS Growth (F1) </th>
<td>39.74%</td>
</tr>
</tbody>
</table>
</div>
'''
soup = BS(HTML, 'html.parser')
for th in soup.find_all('th', attrs={'class': 'alpha', 'scope': 'row'}):
if 'Proj. EPS Growth (F1)' in th.text:
print(th.find_next('td').text)
Answered By - BrutusForcus
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.