RX 6800xt重新进货的刮网

2024-04-26 17:29:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个网页抓取程序,通过百思买检查RX6800XTGPU的可用性。如果程序必须查找一个标签的状态,这将是一个非常简单的程序,但是由于在百思买上,订购按钮是状态所在的位置,这使得事情变得更加复杂。我正在努力想办法让程序检查是否存在这种状态。下面是按钮和卡的可用性

button class="btn btn-primary btn-lg btn-block btn leading-ficon add-to-cart-button"库存
button class=btn btn-secondary btn-lg btn-block add-to-cart-button查找存储
button class="btn btn-disabled btn-lg btn-block add-to-cart-button"即将上市且缺货

我希望代码接受“库存”和“查找商店”作为可用卡。我相信我需要以某种方式将其设置为if语句来检查button类,但我不确定如何做到这一点。下面是我的项目代码

import time
import requests
from bs4 import BeautifulSoup
from playsound import playsound

#Spoofing the user agent request
def get_page_html(url):
    headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"}
    page = requests.get(url, headers=headers)
    return page.content

#Loading parser and the scraping agent
def check_item_in_stock(page_html):
    soup = BeautifulSoup(page_html, 'html.parser')
    out_of_stock_div = soup.find("button", {"class": "btn btn-disabled btn-lg btn-block add-to-cart-button"})
    return out_of_stock_div.txt == "Sold Out"

#Checking the cards
def check_XFX_Reference_RX6800xt():
    url = "https://www.bestbuy.com/site/xfx-amd-radeon-rx-6800xt-16gb-gddr6-pci-express-4-0-gaming-graphics-card-black/6441226.p?skuId=6441226"
    page_html = get_page_html(url)
    if check_item_in_stock(page_html):
        print("Best Buy - XFX Reference RX 6800XT: In stock")
    else:
        print("Best Buy - XFX Reference RX 6800XT: Out of stock")

while True:
    check_XFX_Reference_RX6800xt()```

Tags: toimport程序addurlhtmlcheckstock
1条回答
网友
1楼 · 发布于 2024-04-26 17:29:01

return out_of_stock_div.txt == "Sold Out"如果按钮的文本“售罄”,则返回True

因此,您需要在此处反转输出:

if check_item_in_stock(page_html):
    print("Best Buy - XFX Reference RX 6800XT: In stock")
else:
    print("Best Buy - XFX Reference RX 6800XT: Out of stock")

所以他们这样读:

if check_item_in_stock(page_html):
    print("Best Buy - XFX Reference RX 6800XT: Out of stock")
else:
    print("Best Buy - XFX Reference RX 6800XT: In stock")

相关问题 更多 >