简体   繁体   English

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

[英]using beatifulsoup4 to scrape a specific part of html code

I am wanting to make a variable equal the 1.65 towards the end of the html code. 我想在HTML代码结尾处使变量等于1.65。 Currently if i was to run my code it will print "price-text". 当前,如果我要运行我的代码,它将打印“ price-text”。 Any help to be able to swap it to print "1.65" would be great. 能够将其交换以打印“ 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 HTML代码

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)

The most popular is property .text 最受欢迎的是property .text

price_text.span.text

But there are other properties and methods 但是还有其他属性和方法

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

Documentation for method get_text() 方法get_text()的文档

Full working code 完整的工作代码

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM