How to Scrape Store Locations from using Python 3

In this tutorial, we will show you how to extract the details of store locations available at, one of the largest retail stores in the U.S.

We’ll search for store locations based on a zip code and extract the following data:

  1. Store Name
  2. Store ID
  3. Distance (in miles) from a given zip code
  4. Address
  5. Zip Code
  6. City
  7. Phone Number

The total number of Walmart stores in the USA is 4,674 . Download dataset of all Walmart stores in US and more from our Data Store

Take a look at the screenshot below annotated with the data we will be extracting from Walmart.


You can extract more details from the store detail page such as timings, days open, departments, and services. But for now, we’ll keep it simple and stick to these fields.

Rendering the Data

Open any browser (we are using Chrome) and go to the link This is the link to search for Walmart store locations listed for the zip code ‘20005’ and within a radial distance of 50 miles.

Right-click on any link on the page and choose – Inspect Element. The browser will open a toolbar and show the HTML Content of the Web Page, formatted nicely. Click  Clear Clear  on the Network panel to clear all requests from the Request table.

Then enter the desired zip code you will need and click on the Search option. Let’s choose the zip code ‘20005’ for now. Select XHR and click on the request:


You’ll see the Request URL-


Open the link in a new tab and you’ll see the data in the raw form. To view the data in JSON format, download the extension JSON formatter.

Building The Scraper

We will use Python 3 for this tutorial. The code won’t run if you are using Python 2.7. You need Python 3 and PIP installed on your computer.

Most UNIX operating systems like Linux and Mac OS comes with Python pre-installed. But, not all the Linux Operating Systems ship with Python 3 by default.

To check your python version open a terminal (in Linux and Mac OS) or Command Prompt (on Windows) and type

python --version

and press enter. If the output looks something like Python 3.x.x, you have Python 3 installed. If it says Python 2.x.x you have Python 2. If it prints an error, you don’t probably have python installed.

If you don’t have Python 3, install it first.

Install Python 3 and Pip

Here is a guide to install Python 3 in Linux

Mac Users can follow this guide –

Windows Users go here –

Install Packages

The Code

import csv
import requests
import json
import argparse
import traceback

def locate_stores(zip_code):
    Function to locate walmart stores
    url = ""%(zip_code)
    headers = { 'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
                'accept-encoding':'gzip, deflate, br',
                'user-agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36'
    stores = []
    print("retrieving stores")
    for retry in range(10):
            get_store = requests.get(url, headers=headers, verify=False)
            store_response = get_store.json()
            stores_data = store_response.get('payload',{}).get("storesData",{}).get("stores",[])
            if not stores_data:
                print("no stores found near %s"%(zip_code))
                return []
            print("processing store details")
            #iterating through all stores
            for store in stores_data:
                store_id = store.get('id')
                display_name = store.get("displayName")
                address = store.get("address").get("address")
                postal_code = store.get("address").get("postalCode")
                city = store.get("address").get("city")
                phone = store.get("phone")
                distance = store.get("distance")

                data = {
            return stores
    return []   

if __name__=="__main__":
    argparser = argparse.ArgumentParser()
    argparser.add_argument('zip_code',help = 'zip code to search')
    args = argparser.parse_args()
    zip_code = args.zip_code
    scraped_data = locate_stores(zip_code)
    if scraped_data:
        print ("Writing scraped data to %s_stores.csv"%(zip_code))
        with open('%s_stores.csv'%(zip_code),'wb') as csvfile:
            fieldnames = ["name","store_id","distance","address","zip_code","city","phone"]
            writer = csv.DictWriter(csvfile,fieldnames = fieldnames,quoting=csv.QUOTE_ALL)
            for data in scraped_data:

Execute the full code with the script name followed by the zip code:

python3 zipcode

As an example, to find all the Walmart stores in and near Boston, Massachusetts we would put the argument as 20005 for zip code:

python3 20005

You should get a file called 20005_stores.csv which will be in the same folder as the script. The output file will look similar to this.

Walmart Store Locations Scraper Results

You can download the code at  Let us know in the comments how this scraper worked for you.

The total number of Walmart stores in the USA is 4,674 . Download dataset of all Walmart stores in US and more from our Data Store

Known Limitations

This code should work for extracting details of Walmart stores for all zip codes available on If you want to learn how to scrape the details of thousands of pages you should read Scalable do-it-yourself scraping – How to build and run scrapers on a large scale and How to prevent getting blacklisted while scraping.

If you need professional help with scraping complex websites, contact us by filling up the form below

We can help with your data or automation needs

Turn the Internet into meaningful, structured and usable data

Please DO NOT contact us for any help with our Tutorials and Code using this form or by calling us, instead please add a comment to the bottom of the tutorial page for help

Disclaimer: Any code provided in our tutorials is for illustration and learning purposes only. We are not responsible for how it is used and assume no liability for any detrimental usage of the source code. The mere presence of this code on our site does not imply that we encourage scraping or scrape the websites referenced in the code and accompanying tutorial. The tutorials only help illustrate the technique of programming web scrapers for popular internet websites. We are not obligated to provide any support for the code, however, if you add your questions in the comments section, we may periodically address them.








Posted in:   Location Data Analysis, Web Scraping Tutorials

Turn the Internet into meaningful, structured and usable data   

ScrapeHero Logo

Can we help you get some data?