DEV Community

Eduardo Alencar
Eduardo Alencar

Posted on • Edited on

Web Scraping Tutorial - Elixir API Server with a Chrome Extension

Image description

Imagine a site cars classification and You should follow a change the prices the determinate Vehicle Models. I called the fake site โ€˜FakeCarsโ€™, the site with a lot system anti-bots systems. Difficult to use Web Scraping default, but it doesn't impossible!

We choose models for price tracking with the idea of creating a Chrome Extension to reading the total number of ads for the page and sending for our API Server. We analyze their prices week-by-week.

Using stack for Price Tracking:

Coding Chrome Extension

Structure for a chrome extension
Image description

Our code
manifest.json v.3

Image description

js/background.js
Image description

js/popup.js

Image description

js/fakecars_contentscript.js

Image description
Image description

The JSON structure sent to API Server a for each page read.

Image description

Coding API server

1 - Creating the Phoenix app
Image description

2 - Configure in config/dev.exs your PostgreSQL login.

Image description

  • Creating ad model
$ mix phx.gen.schema Vehicle.Ad vehicle_ads ad_date:utc_datetime ad_id:string ad_source:string city:string km:integer price:integer state:string vehicle_brand:string vehicle_model:string vehicle_version:string year_manufacture:integer year_model:integer active:boolean
Enter fullscreen mode Exit fullscreen mode

Image description

  • Create the database and run migrate
$ mix ecto.create
$ mix ecto.migrate
Enter fullscreen mode Exit fullscreen mode
  • Configure in 'lib/ads_api_web/route.ex' :
    Image description

  • Finally, we create api_controller.ex in 'lib/ads_api_web/controllers/' for consuming the JSON received and saving it in the database.

Top comments (0)