首页 > 解决方案 > 试图抓取网页。下载的 html 代码与现场代码略有不同

问题描述

我是网络抓取的新手,我正在尝试为网站 pokemoncenter.com 构建一个非常基本的股票跟踪器。访问直播网站上商品的产品页面时,添加到购物车按钮显示为:

<button type="button" class="jsx-2748458255 product-add btn btn-secondary">Add to Cart</button>

当商品缺货时,按钮为:

<button type="button" disabled="" class="jsx-2748458255 product-add btn btn-tertiary disabled">Out of Stock</button>

但是每当我尝试抓取网站时,无论该项目是否有库存,按钮都是:

<button class="jsx-2748458255 product-add btn btn-tertiary disabled" disabled="" type="button"></button>

所以本质上,当我使用 requests.get() 下载 html 代码时,它总是显示为缺货。

import bs4
from bs4 import BeautifulSoup as soup
from urllib.request import urlopen, Request 
import requests
 
page_url = "https://www.pokemoncenter.com/product/701-00364/primal-groudon-poke-plush-17-3-4-in"

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}

req = requests.get(page_url, headers = headers)

page_soup = soup(req.text, "html.parser")

#Find add to cart button
divs = page_soup.findAll("div", {"class" : "jsx-829839431 product-col"})
button = str(divs[1].find("button", {"class" : "jsx-2748458255"}))


#Check if button is disabled or not
if (button.find('disabled') != -1): 
    print("Out of Stock")
else:
    print("In Stock")

有货示例:https
://www.pokemoncenter.com/product/701-00364/primal-groudon-poke-plush-17-3-4-in 缺货示例:https ://www.pokemoncenter.com/产品/701-06558/gigantamax-pikachu-poke-plush-17-in

标签: pythonhtmlbeautifulsoup

解决方案


正如goalie1998 所提到的,该站点可以使用javascript 来仅首先加载必要的图像以减少初始加载时间。您可能仍然可以使用Selenium来抓取该网站,因为它可以模仿浏览器的行为。


推荐阅读