我试过把每根线都换掉,但都没法用。我可以得到<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"
我相信你有两个选择:
1-在父
span
标记上使用regex只提取数字。你知道吗2-使用
decompose()
函数从树中删除子span
标记,然后提取文本,如下所示:不必使用
x.text
获取文本,您可以使用x.find_all(text=True, recursive=False)
来获取节点的所有顶级文本(在字符串列表中),而不必进入子节点。以下是使用您的数据的示例:以下是您的方法:
输出:
相关问题 更多 >
编程相关推荐