Skip to main content

Webparsers.com

As the undisputed leader among the world’s top 100 largest companies, Walmart is a major force in the e-commerce industry. Walmart’s massive scale means its data essentially serves as a real-time economic indicator that you can leverage to anticipate market shifts and consumer behavior changes.

In this blog post, we’ll guide you through the process of scraping Walmart data using Python. We’ll delve into critical aspects of web scraping Walmart, including initial setup, strategies to avoid getting blocked, techniques for identifying and extracting desired data, and methods for delivering product data in a CSV file.

Why Scrape Walmart?

Scraping Walmart provides product descriptions, pricing details, and customer reviews at a large scale. Having a significant amount of scraped data, you can analyze market trends, monitor pricing fluctuations, and understand consumer preferences:

  • Pricing intelligence
  • Product catalog mapping
  • Competitor analysis

In turn, you can better align your offerings with market demands, often using proxies to ensure smooth data extraction.

The implementation of Walmart web scraping provides a steady stream of real-time, high-volume data at preferred intervals—daily, hourly, or whenever you need it. Such scale and timing are only feasible with programmatic product data collection, which eliminates manual checkups as impractical.

1. Set Up the Environment

Setting up your Python environment is the first step to scraping Walmart product data. Start by downloading Python from the official website and installing it on your computer. Next, you’ll want to set up a package manager called pip, which will allow you to easily install the required Python packages for scraping Walmart. To do so, use the following command:

bash

python -m pip install requests bs4 pandas

This command will install three libraries – Requests, BeautifulSoup 4, and Pandas. Let’s quickly overview what each of them will do:

  • Requests is a Python library that allows you to send HTTP requests. It will be used to make network requests to the Walmart website and retrieve the product page.
  • BeautifulSoup 4 is also a Python library. It’s used for web scraping purposes, such as pulling the data out of HTML and XML files. It will be especially handy to parse the HTML content and scrape product data.
  • Pandas is a Python library that is used for data manipulation and analysis. We’ll use this library for storing and exporting the scraped data into CSV format.

With all the necessary packages installed, it’s time to start writing the script.

2. Fetch Walmart Product Page

Start by importing the necessary libraries following the below:

python

import requests
from bs4 import BeautifulSoup
import pandas as pd

Next, let’s try and scrape Walmart’s iPhone 14 product page: https://www.walmart.com/ip/AT-T-iPhone-14-128GB-Midnight/1756765288

python

response = requests.get("https://www.walmart.com/ip/AT-T-iPhone-14-128GB-Midnight/1756765288")
print(response.status_code)

Once you run this code, you’ll likely see a status code of 200. Let’s add a few lines of code to parse the content of the response to validate if it’s working properly. Use BeautifulSoup to do that:

python

soup = BeautifulSoup(response.content, 'html.parser')
print(soup.get_text())
```

In the best-case scenario, you might get an HTML of the web page. However, it's more likely that you'll get something like that:
```
Robot or human?
Activate and hold the button to confirm that you're human. Thank You!
Try a different method
Terms of Use
Privacy Policy
Do Not Sell My Personal Information
Request My Personal Information
©2024 Walmart Stores, Inc.

Let’s dissect what happened here. It’s apparent that Walmart has blocked the script, and a CAPTCHA page has been displayed to prevent you from accessing the product using a script. Nevertheless, that shouldn’t stop you, as there are alternative approaches to overcome this challenge, and we’ll explore them in the subsequent section.

Avoiding Detection Using Headers

To prevent detection, you can include a User-Agent header with the request, which websites often use to determine what kind of device is browsing a particular URL. To obtain this header, web browser developer tools can be used. To access them, follow the steps below:

  1. Open the Chrome browser and navigate to the Walmart product page.
  2. Right-click on the page and select Inspect to open the developer tools.
  3. Click on the Network tab.
  4. Refresh the page (F5 or Ctrl+R).
  5. Click on the first item in the list of requests that appears in the Network tab.

In the Headers section of the request, you’ll see the User-Agent header. You should then copy its value and use it like in the example below:

python

walmart_product_url = 'https://www.walmart.com/ip/AT-T-iPhone-14-128GB-Midnight/1756765288'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'}

response = requests.get(walmart_product_url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')
print(soup.prettify())

Now, once you run this script again, you’ll see the correct product page HTML.

3. Extract Walmart Product Information with BeautifulSoup

Before getting to the scraping part, you need to understand how to locate the data you want to extract. For this tutorial, we’ll scrape Walmart prices and titles. To find these elements, you can inspect the structure of the page using developer tools like before.

Selecting & Scraping Product Title

To find the title using developer tools, select the title of the product with your cursor, right-click on it, and choose Inspect. You should be able to locate the title within the HTML structure.

The product title is enclosed within an h1 tag, which provides a clear indication of how to reference it in the code. To extract the product title, we’ll utilize BeautifulSoup, a widely-used and user-friendly library for web scraping. You can easily instruct BeautifulSoup to scrape the title text using the following code:

python

title = soup.find("h1").text

Selecting & Scraping Product Price

If you want to scrape the product price, the approach will be similar. First, locate the price on the website, hover your mouse over it, right-click, and select Inspect.

You’ll see that the price is in a span tag. To select this tag, you can use BeautifulSoup selector as before:

python

price_element = soup.find("span", {"itemprop": "price"})
price = price_element.text if price_element is not None else ""

Notice that an extra dictionary object is being passed to the find method this time. This tells BeautifulSoup to grab the exact span element using the element’s property. Store this data in a list as per the example below:

python

product_data = [{
    "title": title,
    "price": price,
}]

4. Export Data to a CSV File Using the Pandas Module

The product_data list can be used to store the results in a CSV file. This will be much more useful than the HTML format, as you’ll be able to open it in Excel. The Pandas library will help you do that.

Start by passing the scraped product list to the data frame:

python

df = pd.DataFrame(product_data)

Finally, store the data frame in a CSV file named result.csv in the current directory using the following code:

python

df.to_csv("result.csv", index=False)

5. Full Source Code

The process of scraping name and price data should be pretty clear by now. However, you might be wondering if you can extract multiple products at once. You can certainly do so by slightly modifying the source code.

Simply use a for loop to iterate over the product URLs:

python

import requests
from bs4 import BeautifulSoup
import pandas as pd

product_urls = [
    "https://www.walmart.com/ip/AT-T-iPhone-14-128GB-Midnight/1756765288",
    "https://www.walmart.com/ip/Straight-Talk-Apple-iPhone-15-Pro-128GB-Blue-Prepaid-Smartphone/5060213862"
]

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
}

product_data = []

for url in product_urls:
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.content, 'html.parser')
    print(soup.text)
    title = soup.find("h1").text
    price_element = soup.find("span", {"itemprop": "price"})
    price = price_element.text if price_element is not None else ""
    product_data.append({
        "title": title,
        "price": price,
    })

df = pd.DataFrame(product_data)
df.to_csv("result.csv", index=False)

As you can see, the code is pretty self-explanatory. The code is looping over the product_urls, which contain target product links from Walmart’s website. Afterward, the code parses each product page using BeautifulSoup and stores the result in the product_data list. Once the products are scraped, Pandas data frame stores the product data in a CSV file.

6. Proxy Integration

Let’s take the code and add proxies to mask your actual IP address to avoid IP-based blocking. Proxies allow you to distribute requests across multiple proxy IPs, which increases scraping speed. Additionally, proxies help bypass geo-restrictions by making requests appear to come from different geographic locations, giving you access to region-specific content.

You can provide your proxy IP and proxy authentication credentials within the get() function.

Create a dictionary variable with proxy details, including address (host), port, and authentication credentials. To define a proxy, use the following syntax: protocol://username:password@host:port.

Let’s add Webparsers Residential Proxies:

python

proxies = {
    "http": "http://USERNAME:PASSWORD@pr.webparsers.io:7777",
    "https": "https://USERNAME:PASSWORD@pr.webparsers.io:7777"
}

Here’s the complete code using proxies:

python

import requests
from bs4 import BeautifulSoup
import pandas as pd

product_urls = [
    "https://www.walmart.com/ip/AT-T-iPhone-14-128GB-Midnight/1756765288",
    "https://www.walmart.com/ip/Straight-Talk-Apple-iPhone-15-Pro-128GB-Blue-Prepaid-Smartphone/5060213862"
]

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'
}

# Proxy configuration
proxies = {
    "http": "http://USERNAME:PASSWORD@pr.webparsers.io:7777",
    "https": "https://USERNAME:PASSWORD@pr.webparsers.io:7777"
}

product_data = []

for url in product_urls:
    # Add the proxy in the request
    response = requests.get(url, headers=headers, proxies=proxies)
    soup = BeautifulSoup(response.content, 'html.parser')
    title = soup.find("h1").text
    price_element = soup.find("span", {"itemprop": "price"})
    price = price_element.text if price_element is not None else ""
    
    product_data.append({
        "title": title,
        "price": price
    })

df = pd.DataFrame(product_data)
df.to_csv("result.csv", index=False)

Scraping Walmart Without Getting Blocked

Using user agents and integrating proxies are reliable methods to avoid being blocked by Walmart when scraping its website. However, for more extensive scraping projects, relying solely on these methods may not be adequate since Walmart will eventually detect your scraping behavior and block your IP addresses.

Indeed, Walmart has implemented advanced anti-bot measures, which are frequently updated and can substantially affect all scraping activities.

To overcome this issue, a more advanced and intricate script is required. Such a script should be able to prevent browser fingerprinting, implement proxy rotation, and mimic human browsing patterns. Moreover, this solution may demand frequent upkeep.

An alternative that can potentially save you from the challenges of dealing with Walmart’s anti-bot measures is to use a service like Webparsers’ Walmart Scraper API (part of our Web Scraper API). The scraper API significantly simplifies product data collection as it takes care of scraping, proxy management, and parsing.

It’s a flexible and efficient alternative that is also easily scalable, allowing you to scrape huge amounts of Walmart product data without getting blocked.

It’s also super easy to implement. Let’s take a look at how the code works:

python

import requests

url = "https://api-marketplace.webparsers.com/walmart/product?product_id=5201029827&store_id=3081"

payload={}
headers = {
   'x-api-key': '<api-key>'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

The provided code leverages Webparsers’ Web Scraper API to extract data, requiring only the source URL, location, and a parsing flag to be set to True. The code uses the requests module to send a post request to Webparsers’ API endpoint, along with the necessary payload and authentication credentials.

The API handles all the complexities of bypassing Walmart’s anti-bot measures and parsing the data, returning a well-structured JSON file that can be further processed or saved as a JSON/CSV file. In production, when saving thousands of these results to cloud storage (S3, GCS, etc.), you can use the Result Aggregator feature to merge files and avoid storage inefficiencies and bottlenecks.

Scraping Methods Comparison

CriteriaManual Scraping (without proxies)Manual Scraping Using ProxiesScraper APIs
Key FeaturesSingle, static IP address; Direct network requests; Local execution environmentIP rotation; Geo-targeting; Request distribution; Anti-detection measuresMaintenance-free infrastructure; CAPTCHA handling; JavaScript rendering; Automatic proxy management
ProsMaximum flexibility; No additional service costs; Complete data pipeline control; Minimal latencyImproved success rate; Reduced IP blocking; Coordinate, city, state-level targeting; AnonymityMinimal maintenance overhead; Built-in error handling; Regular updates for site layout changes; Technical support
ConsHigh likelihood of IP blocks; Regular maintenance; Limited scaling; No geo-targetingAdditional proxy service costs; Manual proxy management; Additional setup; Increased request latencyHigher costs; Fixed customization; API-specific limitations; Dependency on provider
Best ForSmall-scale scraping; Unrestricted websites; Custom data extraction logicMedium to large-scale scraping; Restricted websites; Global targetsEnterprise-level scraping; Complex websites with anti-bot measures; Resource-constrained teams; Quick implementation

Conclusion

Scraping Walmart can provide valuable insights for pricing intelligence, competitor analysis, and market research. While manual scraping with Python and BeautifulSoup is a great starting point, larger projects will benefit from proxy integration or dedicated scraper APIs like Webparsers.

Whether you choose to build your own scraper or leverage a ready-made API solution, the key is selecting the right approach based on your project scale, technical resources, and budget. For enterprise-level scraping Walmart data without the hassle of maintaining infrastructure, Webparsers offers a reliable and scalable solution.