gpt4 book ai didi

python - 如何使用 BeautifulSoup 在 HTML 中提取折扣价

转载 作者:行者123 更新时间:2023-12-04 14:52:06 24 4
gpt4 key购买 nike

我正在尝试练习和学习 BeautifulSoup,所以我尝试对在线商店进行网络解析。但是,我无法提取某些商品的某些价格,因为它们在 HTML 中有两个价格——原价和折扣价。雪上加霜的是,它们属于同一个 div 类和 span 类。

完整的 HTML 链接在这里:https://www.parkoutlet.com.ph/collections/mens-footwear?page=1

如果当前页面没有售罄或折扣价,您可以滚动到其他页面。

HTML Code Here

这是我当前的代码:

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup

url = "https://www.parkoutlet.com.ph/collections/mens-footwear"

# Opening the URL
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")

itemContainers = soup.findAll("div", {"class":"grid__item small--one-half medium-up--one-fifth"}) # Finds all the "Shoe Containers"

for container in itemContainers:
brandContainer = container.findAll("div", class_="product-card__brand") # Brand Name
brand = brandContainer[0].text

nameContainer = container.findAll("div", class_="product-card__name") # Product Name
name = nameContainer[0].text

try:
priceContainer = container.findAll("s", class_="product-card__regular-price") # Regular Price
priceAvail = priceContainer[0].text
print(priceAvail)

except:
noStock = container.findAll("div", class_="product-card__availability") # Sold Out
print("SOLD OUT")

print(brand)
print(name)
print("")

它给了我

₱2,495
NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. TURF (YOUTH)

₱2,495
NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. MG (YOUTH)

₱2,495
NIKE
NIKE JR. PHANTOM VISION 2 CLUB DYNAMIC FIT FG/MG (YOUTH)

SOLD OUT
NIKE
NIKE MERCURIAL VAPOR 13 CLUB NEYMAR JR. TURF

₱4,300
ADIDAS
ADIDAS X 19 3 FIRM GROUND BOOTS

SOLD OUT
NIKE
NIKE PHANTOM VISION 2 ACADEMY DYNAMIC FIT IC

SOLD OUT
NIKE
NIKE DAY BREAK MENS SHOE

₱5,795
NIKE
PG 4 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
PG 4 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
PG4 EP MENS BASKETBALL SHOE

₱5,795
NIKE
PG4 EP MENS BASKETBALL SHOE

₱5,795
NIKE
PG 4 PCG EP BASKETBALL MENS SHOE

₱5,795
NIKE
PG 4 PCG EP BASKETBALL MENS SHOE

₱6,445
NIKE
NIKE ZOOM FREAK 2

SOLD OUT
NIKE
NIKE AIR PRESTO MENS SHOE

SOLD OUT
NIKE
JORDAN DELTA MENS SHOE

SOLD OUT
NIKE
JORDAN DELTA MENS SHOE

SOLD OUT
NIKE
KYBRID S2 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
KYBRID S2 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
NIKE AIR MAX 2090 MENS SHOE

是的。品牌名称和商品名称都正确,但价格不正确。例如,第一个项目的价格应该是 P749,因为这是折扣价。

我目前是初学者,如果您在我的代码中看到除了无法获得折扣价之外的任何错误,请告诉我!

1.) 我如何在原价处于相同的 div 和 class 的情况下获得折扣价

2.) 我怎样才能使我的代码更有效率?有没有更好的方法来处理这个项目?

最佳答案

您可以使用此示例获取正常价格/销售价格/可用性:

import requests
from bs4 import BeautifulSoup

url = "https://www.parkoutlet.com.ph/collections/mens-footwear?page=1"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

for item in soup.select(".grid__item.medium-up--one-fifth"):
brand = item.select_one(".product-card__brand")
name = item.select_one(".product-card__name")

sale_price = ""
normal_price = item.select_one(".product-card__regular-price")
if normal_price:
sale_price = normal_price.find_next().find_next_sibling(text=True)
else:
normal_price = item.select_one(".product-card__availability")

print(brand.get_text(strip=True))
print(name.get_text(strip=True))
print(normal_price.get_text(strip=True))
print(sale_price.strip())
print("-" * 80)

打印:

NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. TURF (YOUTH)
₱2,495
₱749
--------------------------------------------------------------------------------
NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. MG (YOUTH)
₱2,495
₱749
--------------------------------------------------------------------------------
NIKE
NIKE JR. PHANTOM VISION 2 CLUB DYNAMIC FIT FG/MG (YOUTH)
₱2,495
₱749
--------------------------------------------------------------------------------

...

关于python - 如何使用 BeautifulSoup 在 HTML 中提取折扣价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68911211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com