我需要在此 html 页面末尾的“td”标签中提取数字(0.04)。
<div class="boxContentInner">
<table class="values non-zebra">
<thead>
<tr>
<th>Apertura</th>
<th>Max</th>
<th>Min</th>
<th>Variazione giornaliera</th>
<th class="last">Variazione %</th>
</tr>
</thead>
<tbody>
<tr>
<td id="open" class="quaternary-header">2708.46</td>
<td id="high" class="quaternary-header">2710.20</td>
<td id="low" class="quaternary-header">2705.66</td>
<td id="change" class="quaternary-header changeUp">0.99</td>
<td id="percentageChange" class="quaternary-header last changeUp">0.04</td>
</tr>
</tbody>
</table>
</div>
我在 Python 2.8 中使用 BeautifulSoup 尝试了此代码:
from bs4 import BeautifulSoup
import requests
page= requests.get('https://www.ig.com/au/indices/markets-indices/us-spx-500').text
soup = BeautifulSoup(page, 'lxml')
percent= soup.find('td',{'id':'percentageChange'})
percent2=percent.text
print percent2
结果是 NONE。
错误在哪里?
我查看了https://www.ig.com/au/indices/markets-indices/us-spx-500似乎您在执行时没有搜索正确的 idpercent= soup.find('td', {'id':'percentageChange'})
实际值位于 <span data-field="CPC">VALUE</span>
您可以使用以下方法检索此信息:
percent = soup.find("span", {'data-field': 'CPC'})
print(percent.text.strip())
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句