I need to extract the digits (0.04) in the “td” tag at the end of this html page.
JavaScript
x
25
25
1
<div class="boxContentInner">
2
<table class="values non-zebra">
3
<thead>
4
<tr>
5
<th>Apertura</th>
6
<th>Max</th>
7
<th>Min</th>
8
<th>Variazione giornaliera</th>
9
<th class="last">Variazione %</th>
10
</tr>
11
</thead>
12
<tbody>
13
<tr>
14
<td id="open" class="quaternary-header">2708.46</td>
15
<td id="high" class="quaternary-header">2710.20</td>
16
<td id="low" class="quaternary-header">2705.66</td>
17
<td id="change" class="quaternary-header changeUp">0.99</td>
18
<td id="percentageChange" class="quaternary-header last changeUp">0.04</td>
19
</tr>
20
</tbody>
21
</table>
22
23
</div>
24
25
I tried this code using BeautifulSoup with Python 2.8:
JavaScript
1
14
14
1
from bs4 import BeautifulSoup
2
import requests
3
4
page= requests.get('https://www.ig.com/au/indices/markets-indices/us-spx-500').text
5
soup = BeautifulSoup(page, 'lxml')
6
7
percent= soup.find('td',{'id':'percentageChange'})
8
percent2=percent.text
9
10
11
print percent2
12
13
14
The result is NONE.
Where is the error?
Advertisement
Answer
I had a look at https://www.ig.com/au/indices/markets-indices/us-spx-500 and it seems you are not searching for the right id when doing percent= soup.find('td', {'id':'percentageChange'})
The actual value is located in <span data-field="CPC">VALUE</span>
You can retrieve this information with the below:
JavaScript
1
3
1
percent = soup.find("span", {'data-field': 'CPC'})
2
print(percent.text.strip())
3