如何访问嵌套跨度标签内的数据

时间:2019-06-13 20:38:21

标签: python html beautifulsoup request

我尝试替换每个字符串,但无法正常工作。我可以获取<span>...</span>之间的所有数据,但是如果关闭则无法获取,该怎么办?我曾尝试过替换文本,但是我不能这样做。我对python很陌生。

我也尝试使用for x in soup.find_all('/span', class_ = "textLarge textWhite"),但不会显示任何内容。

相关html:

<div style="width:100%; display:inline-block; position:relative; text- 
align:center; border-top:thin solid #fff; background-image:linear- 
gradient(#333,#000);">
    <div style="width:100%; max-width:1400px; display:inline-block; 
position:relative; text-align:left; padding:20px 15px 20px 15px;">
        <a href="/manpower-fit-for-military-service.asp" title="Manpower 
Fit for Military Service ranked by country">
            <div class="smGraphContainer"><img class="noBorder" 
src="/imgs/graph.gif" alt="Small graph icon"></div>
        </a>
        <span class="textLarge textWhite"><span 
class="textBold">FIT-FOR-SERVICE:</span> 18,740,382</span>
    </div>
    <div class="blockSheen"></div>
</div>

相关的python代码:

for y in soup.find_all('span', class_ = "textBold"):
    print(y.text) #this gets FIT-FOR-SERVICE:
for x in soup.find_all('span', class_ = "textLarge textWhite"):
    print(x.text) #this gets FIT-FOR-SERVICE: 18,740,382 but i only want the number 

预期结果"18,740,382"

3 个答案:

答案 0 :(得分:0)

我相信您在这里有两个选择:

1 -在父span标记上使用正则表达式仅提取数字。

2 -使用decompose()函数从树中删除子span标签,然后提取文本,如下所示:

from bs4 import BeautifulSoup

h = """<div style="width:100%; display:inline-block; position:relative; text-
align:center; border-top:thin solid #fff; background-image:linear-
gradient(#333,#000);">
    <div style="width:100%; max-width:1400px; display:inline-block;
position:relative; text-align:left; padding:20px 15px 20px 15px;">
        <a href="/manpower-fit-for-military-service.asp" title="Manpower
Fit for Military Service ranked by country">
            <div class="smGraphContainer"><img class="noBorder"
src="/imgs/graph.gif" alt="Small graph icon"></div>
        </a>
        <span class="textLarge textWhite"><span
class="textBold">FIT-FOR-SERVICE:</span> 18,740,382</span>
    </div>
    <div class="blockSheen"></div>
</div>"""

soup = BeautifulSoup(h, "lxml")
soup.find('span', class_ = "textLarge textWhite").span.decompose()
res = soup.find('span', class_ = "textLarge textWhite").text.strip()

print(res)
#18,740,382

答案 1 :(得分:0)

这是您的方法:

soup.find('span', {'class':'textLarge textWhite'}).find('span').extract()
output = soup.find('span', {'class':'textLarge textWhite'}).text.strip()

输出:

  

18,740,382

答案 2 :(得分:0)

您可以使用x.text而不是使用x.find_all(text=True, recursive=False)来获取文本,而这将为您提供节点的所有顶级文本(在字符串列表中),而无需进入子级。这是使用您的数据的示例:

for x in soup.find_all('span', class_ = "textLarge textWhite"):
    res = x.find_all(text=True, recursive=False)
    # join and strip the strings then print
    print(" ".join(map(str.strip, res)))

#outputs: '18,740,382'