使用beatifulsoup4来刮取html代码的特定部分

2024-04-18 00:13:38 发布

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

我想让一个变量等于html代码末尾的1.65。目前,如果我要运行我的代码,它将打印“价格文本”。任何帮助能够交换到打印“1.65”将是伟大的。你知道吗

<div class="priceText_f71sibe"><span class="size14_f7opyze medium_f1wf24vo priceTextSize_frw9zm9" data-automation-id="price-text">1.65</span></div>

html code

uClient.close()
page_soup = soup(page_html, "html.parser")
price_texts = page_soup.findAll("div",{"class":"priceText_f71sibe"})
price_text = price_texts[0]
a =price_text.span["data-automation-id"]
print (a)

Tags: 代码textdividdatahtmlpageprice
1条回答
网友
1楼 · 发布于 2024-04-18 00:13:38

最流行的是属性.text

price_text.span.text

但是还有其他的属性和方法

price_text.span.text
price_text.span.string
price_text.span.getText()
price_text.span.get_text()

方法get_text()的文档

完整工作代码

from bs4 import BeautifulSoup

html = '<div class="priceText_f71sibe"><span class="size14_f7opyze medium_f1wf24vo priceTextSize_frw9zm9" data-automation-id="price-text">1.65</span></div>'

soup = BeautifulSoup(html, "html.parser")

price_texts = soup.findAll("div",{"class":"priceText_f71sibe"})
price_text = price_texts[0]
a = price_text.span["data-automation-id"]

print(price_text.span.text)
print(price_text.span.string)
print(price_text.span.getText())
print(price_text.span.get_text())

相关问题 更多 >

    热门问题