ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - feng19/spider_man: SpiderMan,a base-on Broadway fast high-level web crawling & scraping framework for Elixir.
SpiderMan,a base-on Broadway fast high-level web crawling & scraping framework for Elixir. - feng19/spider_man
Visit Site

GitHub - feng19/spider_man: SpiderMan,a base-on Broadway fast high-level web crawling & scraping framework for Elixir.

GitHub - feng19/spider_man: SpiderMan,a base-on Broadway fast high-level web crawling & scraping framework for Elixir.

SpiderMan

Module Version Hex Docs Total Download License Last Updated

SpiderMan,a fast high-level web crawling & scraping framework for Elixir.

Inspired by Crawly(Elixir) and Scrapy(Python).

Usage

This example show how to crawly data from elixir-jobs website by use SpiderMan.

Use in livebook

Run in Livebook

Use in script or mix project

Mix.install([
  {:spider_man, "~> 0.6"},
  {:floki, "~> 0.34"},
  {:nimble_csv, "~> 1.2"}
])

defmodule SpiderList.ElixirJobs do
  @moduledoc false
  use SpiderMan
  require Logger
  alias SpiderMan.Response

  @base_url "https://elixirjobs.net/"

  def run do
    SpiderMan.run_until_zero(__MODULE__, [], 3_000)
  end

  @impl true
  def settings do
    requester_options = [
      base_url: @base_url,
      middlewares: [
        {SpiderMan.Middleware.UserAgent,
         [
           "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
           "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36",
           "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
           "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4389.82 Safari/537.36",
           "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4389.82 Safari/537.36",
           "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
         ]},
        {Tesla.Middleware.Headers,
         [
           {"referer", @base_url},
           {"accept",
            "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"},
           {"accept-encoding", "gzip, deflate"},
           {"accept-language", "zh-CN,zh;q=0.9,zh-TW;q=0.8,en;q=0.7"}
         ]},
        Tesla.Middleware.DecompressResponse
      ]
    ]

    [
      downloader_options: [requester: {SpiderMan.Requester.Finch, requester_options}],
      spider_options: [pipelines: []],
      item_processor_options: [
        storage: [
          {SpiderMan.Storage.ETS, "./data/jobs.ets"},
          {SpiderMan.Storage.CSV,
           file: "./data/jobs.csv",
           headers: [
             :link,
             :title,
             :sub_title,
             :date,
             :workplace,
             :type
           ]}
        ]
      ]
    ]
  end

  @impl true
  def init(state) do
    build_request(@base_url)
    |> set_flag(:first_page)
    |> then(&SpiderMan.insert_request(__MODULE__, &1))

    state
  end

  @impl true
  def handle_response(%Response{env: env, flag: :first_page}, _context) do
    total_page =
      Regex.run(~r/Showing page 1 of (\d+)/, env.body, capture: :all_but_first)
      |> hd()
      |> String.to_integer()

    Logger.info("total: #{total_page}")

    requests =
      Enum.map(2..total_page, fn n ->
        build_request("/?page=#{n}")
        |> set_flag({:list_page, n})
      end)

    handle_list_page(env.body, 1)
    |> Map.put(:requests, requests)
  end

  def handle_response(%Response{env: env, flag: {:list_page, n}}, _context) do
    handle_list_page(env.body, n)
  end

  defp handle_list_page(body, n) do
    Logger.info("processing page #{n}")
    {:ok, document} = Floki.parse_document(body)

    jobs =
      Floki.find(document, ".offers-index")
      |> hd()
      |> Floki.children(include_text: false)
      |> Enum.filter(&match?({"a", _, _}, &1))

    items =
      Enum.map(jobs, fn job ->
        title = Floki.find(job, ".title strong") |> Floki.text() |> String.trim()
        sub_title = Floki.find(job, ".title small") |> Floki.text() |> String.trim()
        link = Floki.attribute(job, "a", "href") |> hd()

        [_, date, _, workplace, _, type] =
          Floki.find(job, ".control .tag")
          |> Enum.map(&(&1 |> Floki.text() |> String.trim()))

        build_item(
          link,
          %{
            link: @base_url <> String.slice(link, 1..-1),
            title: title,
            sub_title: sub_title,
            date: date,
            workplace: workplace,
            type: type
          }
        )
      end)

    %{items: items}
  end
end

SpiderList.ElixirJobs.run()

copy this script and save to elixir_jobs.exs and then start by command:

elixir elixir_jobs.exs

Copyright and License

Copyright (c) 2023 feng19

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory