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

时间:2019-04-10 12:38:53

标签: python web-scraping beautifulsoup

我想在html代码结尾处使变量等于1.65。当前,如果我要运行我的代码,它将打印“ price-text”。能够将其交换以打印“ 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)

1 个答案:

答案 0 :(得分: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())