使用beatifulsoup4来抓取HTML代码的特定部分

问题描述 投票:0回答:1

我想在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)
python web-scraping beautifulsoup
1个回答
1
投票

最受欢迎的是物业.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())
© www.soinside.com 2019 - 2024. All rights reserved.