使用beautifulsoup提取<strong>标记值

2024-10-04 03:23:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从下面的html代码中提取strong和sup标记的值:

[<li class="price-current"><span class="price-current-label"></span>$<strong>299</strong><sup>.99</sup> <a class="price-current-num" href="https://www.newegg.com/xfx-radeon-rx-5600-xt-rx-56xt6df46/p/N82E16814150835?Item=N82E16814150835&amp;buyingoptions=New">(4 Offers)</a><span class="price-current-range"><abbr title="to">–</abbr></span></li>]

enter image description here


Tags: 代码标记htmllicurrentrxpricenum
2条回答
soup3=BeautifulSoup(html,'html.parser')
spans=soup3.findAll('strong')
spans=soup3.findAll('sup')

尝试:

output = []
for soup in data3:
    output.append(soup.find("strong"))
    output.append(soup.find("sup"))

运行此操作后,output列表将从data3的所有元素中获得第一个strongsup元素

如果希望将文本放在标记中,可以在find调用之后使用.content.get_text()。像soup.find("strong").get_text()

有关靓汤的更多帮助,请参见https://www.crummy.com/software/BeautifulSoup/bs4/doc/#find

相关问题 更多 >