In this tutorial, we are going to create our own e-commerce search API with support for both eBay and Etsy without using any external APIs.
With the power ofĀ AutoScraperĀ andĀ Flask, we are able to achieve this goal in fewer than 20 lines of Python code for each site. I recommend reading my last articleĀ about AutoScraper if you havenāt done so yet.
Requirements
Install the required libraries using pip:
pip install -U autoscraper flask
Letās Do It
First, we are going to create a smart scraper to fetch data from eBayās search results page. Letās say we want to get theĀ title,Ā price, andĀ product linkĀ of each item. Using AutoScraper, it would be easily done by just providing some sample data:
Note that if you want to copy and run this code, you may need to update the
wanted_list
.Now letās get the results grouped by scraping rules:
scraper.get_result_similar(url, grouped=True)
From the output, weāll know which rule corresponds to which data, so we can use it accordingly. Letās set some aliases based on the output, remove redundant rules, and save the model so we can use it later:
Note that the rule IDs will be different for you if you run the code.
OK, weāve got eBay covered. Letās add support for Etsy search results too. Weāll start by building its scraper. This time, we will useĀ
wanted_dict
instead of wanted_list
. It will automatically set aliases for us:As the links are generated with a unique ID on Etsy each time, we added one sample product ID to theĀ
wanted_dict
Ā so we can create the link from it. Also, we provided two samples for title and price, as the structure of items on Etsy search result pages is different and we want the scraper to learn them all.After analyzing the output, letās keep our desired rules, remove the rest, and save our model:
Now that we have our scrapers ready, we can create our fully functioning API for both sites in fewer than 40 lines:
Here, we are defining an API with the parameterĀ
q
Ā as our search query. We will get and join eBay and Etsy search results and return them as a response. Note that we are passingĀ group_by_alias=True
Ā to the scraper to get the results grouped by our defined aliases.By running this code, the API server will be up listening on port 8080. So letās test our API by openingĀ
http://localhost:8080/?q=headphone
Ā in our browser: Some results from eBay
Some results from Etsy
Voila! We have our e-commerce API ready. Just replaceĀ
headphone
Ā in the URL with your desired query to get its search results.Final Notes
The final code for this tutorial is available onĀ GitHub.
This is a development setup suitable for developing and testing. Flaskās built-in server is not suitable for production. For production usage, please checkĀ Flaskās deployment options.
This tutorial is intended for personal and educational use. If you want to scrape websites, you can check their policies regarding the scraping bots.
I hope this article is useful and helps to bring your ideas into code faster than ever. Happy coding!