使用海量数据抓取沃尔玛产品数据

Jason Grad
创始人
March 4, 2025

準備好測試高級代理的效能了嗎?

使用海量数据抓取沃尔玛产品数据

Jason Grad
创始人
March 4, 2025

沃尔玛通过其沃尔玛网站提供了大量的产品数据,这是企业、研究人员和营销人员的宝贵资源。无论您是访问沃尔玛的单个产品页面还是浏览多个沃尔玛页面,由于沃尔玛采取了强有力的防抓取措施,收集相关信息都可能具有挑战性。

本文将解释Massive代理如何使绕过这些限制变得更容易,并允许您从特定位置收集沃尔玛产品详细信息。

让我们潜入吧!

沃尔玛产品数据的常见用例

沃尔玛网站包含可用于各种目的的宝贵见解,包括:

  • 产品与市场研究: 分析沃尔玛每个产品页面的数据点,包括评论和评级,以了解消费者的偏好和市场趋势。
  • 竞争对手分析: 深入了解竞争对手的定价和产品策略
  • 价格监控和优化: 跟踪实时价格以调整定价策略并保持竞争力。
  • 库存管理: 监控库存水平和产品可用性,以优化库存和供应链运营。

为什么要在网页抓取中使用代理

代理充当抓取工具和目标网站之间的中介,在网络抓取中起着至关重要的作用。它们提供了几个关键好处,包括:

  1. 避开知识产权禁令: 代理允许您轮换 IP 地址,从而降低网站检测和屏蔽的风险。
  2. 访问受地理限制的内容:某些内容或产品仅在某些地区可用。代理使您看起来好像在其他位置浏览。
  3. 绕过速率限制: 网站通常对来自单个 IP 地址的请求数量施加限制。代理有助于将您的请求分发到多个IP上,从而使您可以避开这些限制。

了解沃尔玛的防刮措施

由于采取了多项保护措施,从沃尔玛抓取数据可能具有挑战性。以下是你可能会遇到的一些常见问题:

  1. 验证码挑战: 沃尔玛使用验证码来屏蔽机器人。尽管这些问题对于人类来说相对容易解决,但对于自动化脚本来说可能很困难。
  2. 速率限制: 沃尔玛限制了你在短时间内可以提出的请求数量。如果您超过此限制,您的访问权限可能会被阻止。
  3. IP 封锁:如果沃尔玛检测到来自单个IP地址的过度抓取活动,它可能会封锁该IP。
  4. 更改页面布局: 沃尔玛经常更新其网页结构。这些更改可能会破坏您的抓取代码,需要您定期对其进行更新。
  5. 动态内容: 沃尔玛搜索页面和其他沃尔玛页面使用JavaScript来动态加载内容,这会使抓取变得更加复杂。

例如,我创建了一个脚本来从沃尔玛的多个产品页面抓取数据。但是,我的刮刀最终被沃尔玛的机器人检测系统屏蔽了,如下图所示。

这个挑战要求我 “长按” 来验证自己的人性,是网站用来防止机器人访问其内容的常用验证码机制。

此时,我的抓取工具无法再访问数据,这既令人沮丧又耗时。但别担心,有解决办法。我们将探讨如何 大规模住宅代理 可以帮助您抓取沃尔玛的产品数据。

沃尔玛抓取的住宅代理与数据中心代理:哪种最有效

我收购沃尔玛已经有一段时间了,你的代理选择决定一切。根据我的经验,住宅代理值得额外付费。他们使用实际互联网服务提供商分配给房主的真实IP,因此在沃尔玛的系统中,你看起来就像普通的购物者在家中浏览。是的,它的成本更高,但是数据质量和不间断的抓取会话使其值得。

数据中心代理很诱人——它们更快、更便宜——但是沃尔玛的反机器人系统已经很擅长发现它们了。

使用大规模代理进行沃尔玛抓取的好处

大规模住宅代理提供了几个关键好处:

  1. 🌐 全球影响力: 访问 195 多个国家/地区——目标城市、邮政编码或 ASN
  2. 🔒 匿名: 数百万个轮换 IP + 可自定义轮换(每个请求或 1-60 分钟)
  3. ⚡ 速度和可靠性: 99.8% 的成功率,<0.7 秒的响应时间,99.9% 的正常运行时间
  4. 💰 经济实惠: 可扩展套餐的起价为 4.49 美元/GB
  5. ✅ 符合道德标准: 完全符合 GDPR/CCPA 的代理,100% 符合道德标准
  6. 🛠️ 支持: 通过 Slack、Skype、电子邮件或票证

Massive 入门

如果你是 Massive 的新手, 注册一个账号。根据您的需求选择套餐。

注意: 我们提供 2 GB 免费试用 对于公司来说。首先, 填写这张表格。如果你需要更多的带宽, 联系我们的销售团队,我们将为您提供帮助。

注册后,前往 大型仪表板 检索您的代理凭证(用户名和密码)。

配置步骤:

请访问 快速入门 用于自定义代理设置的部分:

  • 选择您的首选协议(HTTP、HTTPS 或 SOCKS5)
  • 在两者之间选择 旋转或粘性代理
  • 设置地理定位偏好(国家、州、城市或邮政编码)

配置完成后,您将获得适用于特定用例的即用型 cURL 命令。

有关基于位置的定位和粘性会话等高级功能,请参阅 海量文档。这些文档提供了充分利用大规模住宅代理的分步说明。

通过此设置,您可以使用Massive Proxies从特定区域抓取沃尔玛产品数据。

使用 Python 和大规模代理构建沃尔玛抓取工具

虽然你可以使用沃尔玛抓取器API,但使用正确的用户代理配置构建自己的解决方案可以让你更好地控制抓取过程。让我们探索如何使用 Massive 代理和 Playwright 为沃尔玛的产品数据构建 Python 抓取工具。Playwright 可帮助自动执行浏览器操作并处理动态内容,例如在您滚动时加载更多产品。

使用 巨大 代理,您只需更改代理设置即可从沃尔玛运营的任何位置抓取沃尔玛数据。在本教程中,我们将展示如何抓取产品数据 美国华盛顿

我们将提取每款沃尔玛产品的以下数据:产品名称、评级、评论数量、价格、先前价格(如果有)、配送信息和产品链接。

1。设置你的 Python 环境

首先,请确保您的计算机上安装了 Python。接下来,安装 Playwright 及其必要的浏览器二进制文件:

pip install playwright
playwright install

2。导入所需的库

现在,让我们开始编写脚本。你需要导入以下库:

  • asyncio 用于异步编程。
  • 随机 添加随机延迟。
  • json 保存我们抓取的数据。
  • 异步剧作家 由 Playwright 来控制浏览器并自动抓取。
import asyncio
import random
import json
from playwright.async_api import async_playwright

3.使用大量代理设置启动浏览器

使用代理设置以无头模式启动 Chromium 浏览器。这使您可以使用以下方法绕过沃尔玛的防刮措施 巨大 代理。

async with async_playwright() as p:
    browser = await p.chromium.launch(
        headless=True,
        args=[
            "--disable-blink-features=AutomationControlled",
            "--disable-infobars",
            "--disable-extensions",
            "--disable-popup-blocking",
            "--no-sandbox",
        ],
    )

接下来,设置浏览器上下文以路由流量 巨大 住宅代理:

context = await browser.new_context(
    user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
    locale="en-GB",
    proxy={
        "server": "https://network.joinmassive.com:65535",
        "username": "MASSIVE_USERNAME-country-US-subdivision-WA",
        "password": "MASSIVE_PASSWORD",
    },
    viewport={"width": 1920, "height": 1080},
)

打开新页面并导航到沃尔玛搜索结果:

page = await context.new_page()
await page.goto(
    "https://www.walmart.com/search?q=windows+laptops",
    wait_until="domcontentloaded",
)

此代码会将浏览器定向到沃尔玛的 “Windows笔记本电脑” 搜索结果页面。

4。滚动并提取数据

由于沃尔玛会在您滚动时加载更多产品,因此我们将模拟人形滚动以确保加载所有产品:

async def scroll_and_extract(page):
    previous_height = await page.evaluate("document.body.scrollHeight")
    while True:
        await page.evaluate("window.scrollBy(0, Math.random() * 100 + 300)")
        await asyncio.sleep(random.uniform(1, 2))

        new_height = await page.evaluate("document.body.scrollHeight")
        if new_height == previous_height:
            break
        previous_height = new_height

5。提取产品信息

现在,我们使用 CSS 选择器提取产品名称、价格和评级等详细信息。您可以通过以下方式提取每条信息:

A. 产品名称:

代码片段:

product_name = await product.query_selector('span[data-automation-id="product-title"]')
product_name = await product_name.inner_text() if product_name else "N/A"

B. 当前价格:

代码片段:

price = await product.query_selector(
    'div[data-automation-id="product-price"] div[aria-hidden="true"]'
)
price = await price.inner_text() if price else "N/A"

C. 先前价格:

代码片段:

previous_price = await product.query_selector("div.gray.strike")
previous_price = await previous_price.inner_text() if previous_price else "N/A"

D. 产品评级:

代码片段:

rating = await product.query_selector('span[data-testid="product-ratings"]')
rating = await rating.get_attribute("data-value") if rating else "N/A"

E. 评论数量:

代码片段:

num_reviews = await product.query_selector('span[data-testid="product-reviews"]')
num_reviews = await num_reviews.inner_text() if num_reviews else "N/A"

F. 配送信息:

代码片段:

shipping_info = await product.query_selector('div[data-automation-id="fulfillment-badge"]')
shipping_info = await shipping_info.inner_text() if shipping_info else "N/A"

这是 组合代码 它返回每种产品的所有信息。

async def extract_product_info(product):
    title_selector = 'span[data-automation-id="product-title"]'
    price_selector = 'div[data-automation-id="product-price"] div[aria-hidden="true"]'
    previous_price_selector = "div.gray.strike"
    rating_selector = 'span[data-testid="product-ratings"]'
    reviews_selector = 'span[data-testid="product-reviews"]'
    shipping_info_selector = 'div[data-automation-id="fulfillment-badge"]'
    product_url_selector = 'a[href*="/ip/"]'

    title = await product.query_selector(title_selector)
    product_url_element = await product.query_selector(product_url_selector)
    product_url = (
        await product_url_element.get_attribute("href") if product_url_element else None
    )

    if product_url and "from=/search" in product_url:
        current_price = await product.query_selector(price_selector)
        previous_price = await product.query_selector(previous_price_selector)
        rating = await product.query_selector(rating_selector)
        num_reviews = await product.query_selector(reviews_selector)
        shipping_info = await product.query_selector(shipping_info_selector)

        return {
            "title": await title.inner_text() if title else "N/A",
            "product_url": f"https://www.walmart.com/{product_url}",
            "current_price": (
                await current_price.inner_text() if current_price else "N/A"
            ),
            "previous_price": (
                await previous_price.inner_text() if previous_price else "N/A"
            ),
            "rating": await rating.get_attribute("data-value") if rating else "N/A",
            "num_reviews": await num_reviews.inner_text() if num_reviews else "N/A",
            "shipping_info": (
                await shipping_info.inner_text() if shipping_info else "N/A"
            ),
        }
    return None

6。抓取多页

要抓取多页,我们将找到 “下一页” 按钮,然后点击每个页面

async def scrape_walmart(page, current_page):
    async def product_info_generator(current_page):
        while True:
            print(f"Scraping page {current_page}...")
            await scroll_and_extract(page)

            # Extract product information
            product_elements = await page.query_selector_all(
                'div[role="group"][data-item-id]'
            )

            for product in product_elements:
                product_data = await extract_product_info(product)
                if product_data:
                    yield product_data
            # Check for the "Next Page" button
            next_page_button = await page.query_selector('a[data-testid="NextPage"]')
            if next_page_button:
                await next_page_button.click()
                current_page += 1
            else:
                break

    return product_info_generator(current_page)

7。将数据保存到 JSON 文件中

提取所有数据后,将其保存到 JSON 文件中:

def save_data_to_json(data, filename):
    with open(filename, "w", encoding="utf-8") as json_file:
        json.dump(data, json_file, ensure_ascii=False, indent=4)
    print(f"Product information saved to {filename}")

8。运行 Scraper

以下是启动沃尔玛抓取器的主要功能:

async def main():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=True)
        page = await browser.new_page()
        await page.goto(
            "https://www.walmart.com/search?q=windows+laptops",
            wait_until="domcontentloaded",
        )

        current_page = 1
        product_info_generator = await scrape_walmart(page, current_page)

        with open("walmart_products.json", "w", encoding="utf-8") as json_file:
            json_file.write("[")
            first = True
            async for product in product_info_generator:
                if not first:
                    json_file.write(",\\n")
                json.dump(product, json_file, ensure_ascii=False, indent=4)
                first = False
            json_file.write("\\n]")
            print("Product information saved to walmart_products.json")


asyncio.run(main())

最终结果

你可以在中找到使用大规模代理抓取沃尔玛数据的完整代码 GitHub 要点

当你运行代码时,结果将如下所示:

[
    {
        "title": "14.1in Windows 11 Pro Laptop, 8GB DDR4, 512GB SSD Computer, Intel Celeron, 1920x1080, 1TB Expansion, Silver",
        "product_url": "<https://www.walmart.com/ip/Temlicolo-14-1-Laptop-8GB-RAM-PC-512GB-SSD-Intel-Celeron-N4020C-up-to-2-8GHz-Windows-11-Pro-Webcam-1TB-SSD-Expansion-Silver/1519228026?classType=VARIANT&selectedSellerId=101196098&from=/search>",
        "current_price": "$227.89",
        "previous_price": "$499.99",
        "rating": "4.5",
        "num_reviews": "220",
        "shipping_info": "Free shipping, arrives in 2 days"
    },
    {
        "title": "HP Stream 14 inch Windows Laptop Intel Processor N4120 4GB RAM 64GB eMMC Pink (12-mo. Microsoft 365 included)",
        "product_url": "<https://www.walmart.com/ip/HP-Stream-14-inch-Laptop-Intel-Processor-N4102-4GB-RAM-64GB-eMMC-Pink-12-mo-Microsoft-365-included/443153637?classType=VARIANT&athbdg=L1102&from=/search>",
        "current_price": "$199.00",
        "previous_price": "$229.00",
        "rating": "4",
        "num_reviews": "11,240",
        "shipping_info": "Free pickup today\\nDelivery today\\nFree shipping, arrives today"
    },
    {
        "title": "Jumper 15.6\\" Windows 11 Laptop 4GB DDR4 128GB Rom Computer with Intel Celeron 5205U, Come with 1-Yr Free Office 365",
        "product_url": "<https://www.walmart.com/ip/Jumper-15-6-Laptop-4GB-DDR4-128GB-ROM-Computer-with-Dual-Core-Intel-Celeron-5205U-CPU-1-yr-Office-Free-1366x768-HD/9497006657?classType=VARIANT&selectedSellerId=101078354&from=/search>",
        "current_price": "$199.89",
        "previous_price": "$379.99",
        "rating": "4.2",
        "num_reviews": "6",
        "shipping_info": "Free shipping, arrives in 2 days"
    }
]

检查完整 JSON 文件 所有沃尔玛的 “Windows笔记本电脑” 数据都是从每个可用页面上抓取的。

总结

本文讨论了利用Massive代理如何帮助您提取有价值的数据,同时最大限度地降低被检测和屏蔽的风险。有关代理配置或最佳实践的更多详细信息,请务必访问我们的 官方文档

准备好开始了吗? 立即注册大规模代理 🚀

作者简介
Jason Grad
创始人

我是Massive的联合创始人兼首席执行官。除了在创业公司工作外,我还是一名音乐家、运动员、导师、活动主持人和志愿者。

经常问的问题

+

+

+

+

+

+

+

+

+

+

Ready to test premium proxy performance?