Skip to main content

Scrappe all products and theirs related suppliers existing on Alibaba based on keywords provided by user and save results into a database (Mysql/Sqlite).

Project description

overview image

Alibaba-CLI-Scraper

Is a python package that provides a dedicated CLI interface for scraping data from Alibaba.com. The purpose of this project is to extract products and theirs related suppliers informations from Alibaba.com and store it in a local database (SQLite or MySQL). The project utilizes asynchronous requests for efficient handling of numerous requests and allows users to easily run the scraper and manage the database using a user-friendly command-line interface (CLI).

Features:

  • Asynchronous Scraping: Utilizes asynchronous API of Playwright for efficient handling of numerous pages results.
  • Database Integration: Stores scraped data in a database (SQLite or MySQL) for structured persistence.
  • User-Friendly CLI: Provides easy-to-use commands for running the scraper and managing the database.

Future Enhancements

This project has a lot of potential for growth! Here are some exciting features I'm considering for the future:

  • Data Export: Add functionality to export scraped data to various formats like CSV and Excel spreadsheets for easier analysis and sharing.
  • PostgreSQL Support: Expand database compatibility to include PostgreSQL, giving users more database choices.
  • Retrieval Augmented Generation (RAG): Integrate a RAG system that allows users to ask natural language questions about the scraped data, making it even more powerful for insights.

Installation

Like any other python packages, to avoid any issues, with other packages or depencies installed already installed on your machine, this tool should be installed with pipx to create isolated environments before to run it. But i didn't found a way to allow that. Then you will need to create a virtual environment before to install it with the following commands:

  1. Create virtual environment:

       python -m venv scrapper
    
  2. Activate virtual environment:

       scrapper\Scripts\activate.bat
    
  3. Install scraper package:

       python -m pip install aba-cli-scrapper 
    

Using the CLI Interface

This project provides a user-friendly command-line interface (CLI) built with typer for interacting with the scraper and database.

Available Commands:

Need Help? run any commands followed by --help for detailed informations about its usage and options. For example: aba-run --help will show you all subcommands available and how to use them.

command result 1

Warnings: 1) aba-run is the base command means all other commands that will be introduce bellow are sub-commands and should always be preceded by aba-run. 2) As i'm still working on this packages i'm facing many bugs with async part of this tool i'm using bright data to enhance perfomance when its come to retrieve html pages results asynchronously. So it's could be unvailable due to api key bright data limit reached. Then if for any reason you encounter an issue with async api which is set by default, you can use instead sync api by specifying --sync-api flag cause is works perfecly fine. So let's jump to the tutorial.

Practice make perfect isn't ? So let's get started with a use case example. Let's assume that you want to scrape data about electric bikes from Alibaba.com.

  • scraper: Initiates scraping of Alibaba.com based on the provided keywords. this command takes two required arguments and one optional argument:

    • key_words (required): The search term(s) for finding products on Alibaba. Enclose multiple keywords in quotes.
    • --page-results (required): Usually keys words will results to many pages macthing them. Then you must to indicate how many of them you want to pull out.
    • --html-folder (optional): Specifies the directory to store the raw HTML files. If omitted, a folder with sanitized keywords as name will be automatically created.

    Example:

    aba-run scraper "electric bikes" --html-folder bike_results --page-results 15
    

by default scrapper will use async which is as explained unstable. the if you want to use sync api run: bash aba-run scraper "electric bikes" --html-folder bike_results --page-results 15 --sync-api and voila!

if --html-folder option is not provided, a folder with sanitized keywords as name will be automatically created and should result to electric_bikes as a results folder name. after that bike_results (since you already provided name you wish to have) directory has been created and should contains all html files from alibaba.com matching your keywords.

Then you must initialize a database. Mysql and sqlite are supported.

  • db-init: Creates a new database mysql/sqlite. this command takes one required arguments and six optional arguments(depends on engine you choose):
    • engine (required): Choose either sqlite or mysql.
    • --sqlite-file (optional, SQLite only): The name for your SQLite database file (without the extension).
    • --host, --port, --user, --password, --db-name (required for MySQL): Your MySQL database connection details.
    • --only-with (optional Mysql): If you just want to update some details of your credentials in db_credentials.json file but not all before to initialize an brand new database.

MySQL Example:

aba-run db-init mysql --user "mysql_username" --password "mysql_password" --db-name "alibaba_products" 

Assuming that you have already initialized your database,and you want to created a new one without updating all your credentials, simply run :

aba-run db-init mysql --db-name "alibaba_products" --only-with 

NB: This commands will save your credentials in db_credentials.json file, so when you will need to update your database, simply run aba-run db-update mysql --kw-results bike_results\ to automatically update your database and using your saved credentials

SQLite Use case :

aba db-init sqlite --sqlite-file alibaba_data

As soons as your database has been initialized, you can update it with the scraped data.

  • db-update: add scraped data from html files to your database (you can't use this command twice with same database credentals to avoid UNIQUE CONSTRAINT ERROR).

this command takes two required arguments and two optional arguments: * --db-engine (required): Select your database engine: sqlite or mysql. * --kw-results (required): The path to the folder containing the HTML files generated by the scraper sub command. * --filename (required for SQLite): If you're using SQLite, provide the desired filename for your database. whitout any extension. * --db-name (optional for MySQL): If you're using MySQL,and want to push the data to a different database, provide the desired database name.

MySQL Example:

aba-run db-update  mysql --kw-results bike_results\ 

NB:What if you want to change something while you updating the database? Assuming that you have run another scraping command and you want to save this data in another database name whitout update credential file or rewriting all theses parameter just to change your database name then, simply run aba-run db-update mysql --kw-results another_keyword_folder_result\ --db-name "another_database_name".

SQLite Example:

aba-run db-update  sqlite --kw-results bike_results\ --filename alibaba_data

Contributions Welcome!

I believe in the power of open source! If you'd like to contribute to this project, feel free to fork the repository, make your changes, and submit a pull request. I'm always open to new ideas and improvements.

License

This project is licensed under the Gnu General Public License Version 3.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aba_cli_scrapper-0.1.5.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

aba_cli_scrapper-0.1.5-py3-none-any.whl (61.3 kB view details)

Uploaded Python 3

File details

Details for the file aba_cli_scrapper-0.1.5.tar.gz.

File metadata

  • Download URL: aba_cli_scrapper-0.1.5.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.2 Windows/10

File hashes

Hashes for aba_cli_scrapper-0.1.5.tar.gz
Algorithm Hash digest
SHA256 41bdf2b588d68b8e67961649a3221a51ff936f50596bc4c903c5ccb426189afe
MD5 06bc88c136a9ef5a1c7c750c02ec12e6
BLAKE2b-256 aa463cfccf9827a634432c44b738e4b460a9e934b7f1ab8ed94c84267833a4ca

See more details on using hashes here.

Provenance

File details

Details for the file aba_cli_scrapper-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for aba_cli_scrapper-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 58d8ef9f688b5b9f3e4bbd2f70b07d772074ef5f07945f2e5ab6605f0b7356fa
MD5 dd1c31d820495033737e3056ffe4ed48
BLAKE2b-256 1ffad9e164fd28141ae1d761350e169d8da9d88a495d72675311ce1bef17056d

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page