【问题标题】:Web scraping for a RX 6800xt restock为 RX 6800xt 补货进行网络抓取
【发布时间】:2020-12-06 03:09:44
【问题描述】:

我正在尝试制作一个网络抓取程序,通过百思买检查 RX 6800 XT GPU 的可用性。如果程序必须查找一个标签的状态,这将是一个非常简单的程序,但由于在百思买,订购按钮就是状态所在的位置,这使事情变得更加复杂。我正在努力弄清楚如何让程序检查此状态的存在。下面是按钮和卡片的可用性。

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 语句以检查按钮类,但我不确定如何执行此操作。下面是我的项目代码。

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

【问题讨论】:

  • 如果 soup.select_one('.add-to-cart-button') 在每种情况下都返回正确的匹配项,那么只需检查每个场景中是否存在额外的单个唯一类匹配节点中的多值类。例如:如果节点类包含“btn-disabled”,则即将推出且缺货,否则它是可用卡。

标签: python python-3.x web-scraping beautifulsoup


【解决方案1】:

如果按钮的文本是“Sold Out”,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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多