【发布时间】:2019-06-13 20:38:21
【问题描述】:
我已尝试替换每个字符串,但无法使其正常工作。我可以获取<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"
【问题讨论】:
标签: python html beautifulsoup request