BeautifulSoup:如何提取封装在多个 div/span/id 标签中的文本

威廉

我需要在此 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。

错误在哪里?

小水电3r

我查看了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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

提取 STRONG 标签中的文本

如何使用xpath提取多个标签的html脚本中的文本

BeautifulSoup 如何从 <a> 标签中获取文本

xpath:如何提取“strong”标签中的文本?

从HTML标签和纯文本(未包装在标签中)提取文本

如何从beautifulSoup中提取多个html标签?

如何使用beautifulsoup获取包含在包含多个子标签的标签中的文本?

如何从包含标签文本的<svg>标签中获取文本

如何使用WPF将文本包装在标签中?

BeautifulSoup:提取不在给定标签中的文本

使用Python中的BeautifulSoup 4从div标签中提取文本

使用BeautifulSoup从文本中删除标签

BeautifulSoup在特定标签中查找文本

Beautifulsoup - 在 div 文本中添加 <br> 标签?

在文本中查找和提取主题标签

从XML提取的文本中的标签带

提取imacros中的differents标签的htm文本

从Python中的脚本标签提取文本

从Perl中的HTMl / XML标签提取文本

使用pugiXML提取文本标签中的段落

bs4 从标签中的多个跨度中提取文本

Python:从XML树中的标签内部的标签中提取文本

使用beautifulsoup在标签内搜索文本,并在标签后返回标签中的文本

BeautifulSoup:用其他标签中的文本替换锚文本

如何在 BeautifulSoup4 中替换嵌套标签的文本?

如何在BeautifulSoup中查找给定文本的标签名称

如何使用beautifulsoup和python在span标签中获取文本

如何使用 Selenium 和 BeautifulSoup 从标签中获取文本

如何在beautifulsoup中的特定p标签后获取文本?