Python 我如何使用Beautiful soup从本页获取价格?

Python 我如何使用Beautiful soup从本页获取价格?,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我正试图从中获得价格(即152美元)。我在find_all方法中尝试了不同的标记组合,但得到的只是空列表。我做错了什么 u = 'https://www.dianeslingerie.com/product/serie-piana-short-sleeve-tunic-by-mey/' r = requests.get(url) c = r.content soup = BeautifulSoup(c, "html.parser") soup.find_all('div', {'class':'

我正试图从中获得价格(即152美元)。我在find_all方法中尝试了不同的标记组合,但得到的只是空列表。我做错了什么

u = 'https://www.dianeslingerie.com/product/serie-piana-short-sleeve-tunic-by-mey/'
r = requests.get(url)
c = r.content
soup = BeautifulSoup(c, "html.parser")
soup.find_all('div', {'class':'summary-container'})

这应该满足您的要求:

import requests
from bs4 import BeautifulSoup

url = 'https://www.dianeslingerie.com/product/serie-piana-short-sleeve-tunic-by-mey/'

r = requests.get(url)

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

price = soup.find('span', {'class': 'woocommerce-Price-amount amount'})

print(price.text)
要实现这一点,您可能需要检查页面,并查找一个类、一个id或一个html标记,该类、id或标记对于您想要刮取的对象是唯一的

在这种情况下,“woocommerce价格金额”类仅出现在页面的价格中:

如我们所见,它位于span标记内,因此我们将其与之前找到的类一起使用,并获得以下输出:

$152.00

谢谢很多时候,即使我尝试使用正确的类,它也不会返回任何结果。例如,在给定的示例中,如果我想获取项目名称,我应该在“h1”标记中获取它,并使用类“product\u title entry title fusion responsive typography computed”。但我什么也得不到。你能告诉我为什么吗?老实说,我不知道为什么它没有返回,但我可以通过另一种方式实现:
name=soup.find('div',{'class':'summary container')).find('h1')
import bs4
import requests
u = 'https://www.dianeslingerie.com/product/serie-piana-short-sleeve-tunic-by-mey/'
r = requests.get(u)
c = r.content
soup = bs4.BeautifulSoup(c, "html.parser")
price = soup.find("span", {"class": "woocommerce-Price-amount amount"})
print(price.get_text()) # $152.00