如何使用beautifulsoup在另一个标签内的span标签内获取文本?

时间:2018-08-15 12:41:22

标签: html python-3.x web-scraping beautifulsoup

如何获取所有具有class =“ no-wrap文本右流通供应”标签的值?我使用的是:

$pattern = '/^(\d+|5\+)$/';
preg_match($pattern, '1'); // Match
preg_match($pattern, '5+'); // Match
preg_match($pattern, '10+'); // No match

text=[ ] text=(soup.find_all(class_="no-wrap text-right circulating-supply")) 的输出:

text[0]

我只想提取数值。

一个实例的示例:

'\n\n17,210,662\nBTC\n'

谢谢。

2 个答案:

答案 0 :(得分:2)

如果所有元素都具有相似的HTML结构,请尝试以下操作以获取所需的输出:

args...

答案 1 :(得分:1)

这可能看起来有些矫kill过正,您可以使用regex提取数字

from bs4 import BeautifulSoup
html = """<td class="no-wrap text-right circulating-supply" data-sort="17210662.0">
            <span data-supply="17210662.0">
            <span data-supply-container="">
            17,210,662
            </span>
            <span class="hidden-xs">
            BTC
            </span>
            </span>
        </td>"""
import re
soup = BeautifulSoup(html,'html.parser')
coin_value =  [re.findall('(\d+)', node.text.replace(',','')) for node in soup.find_all(class_="no-wrap text-right circulating-supply")]
print coin_value

打印

[[u'17210662']]
相关问题