scraper

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2020 License: MIT Imports: 14 Imported by: 3

README

scraper

A configuration based, HTML ⇒ JSON API server

Features
  • Single binary
  • Simple configuration
  • Zero-downtime config reload with kill -s SIGHUP <scraper-pid>
Quick Example

Given google.json

{
  "/search": {
    "url": "https://www.google.com/search?q={{query}}",
    "list": "#res div[class=g]",
    "result": {
      "title": "h3 a",
      "url": ["h3 a", "@href", "query-param(q)"]
    }
  }
}
$ scraper google.json
2015/05/16 20:10:46 listening on 3000...
$ curl "localhost:3000/search?query=hellokitty"
[
  {
    "title": "Official Home of Hello Kitty \u0026 Friends | Hello Kitty Shop",
    "url": "http://www.sanrio.com/"
  },
  {
    "title": "Hello Kitty - Wikipedia, the free encyclopedia",
    "url": "http://en.wikipedia.org/wiki/Hello_Kitty"
  },
  ...
Configuration
{
  <path>: {
    "url": <url>
    "list": <selector>,
    "result": {
      <field>: <extractor>,
      <field>: [<extractor>, <extractor>, ...],
      ...
    }
  }
}
  • <path> - Required The path of the scraper
    • Accessible at http://<host>:port/<path>
    • You may define path variables like: my/path/:var when set to /my/path/foo then :var = "foo"
  • <url> - Required The URL of the remote server to scrape
    • It may contain template variables in the form {{ var }}, scraper will look for a var path variable, if not found, it will then look for a query parameter var
  • result - Required represents the resulting JSON object, after executing the <extractor> on the current DOM context. A field may use sequence of <extractor>s to perform more complex queries.
  • <extractor> - A string in which must be one of:
    • a regex in form /abc/ - searches the text of the current DOM context (extracts the first group when provided).
    • a regex in form s/abc/xyz/ - searches the text of the current DOM context and replaces with the provided text (sed-like syntax).
    • an attribute in the form @abc - gets the attribute abc from the DOM context.
    • a query param in the form query-param(abc) - parses the current context as a URL and extracts the provided param
    • a css selector abc (if not in the forms above) alters the DOM context.
  • list - Optional A css selector used to split the root DOM context into a set of DOM contexts. Useful for capturing search results.
Cli version

A cli version scrapercli is provided to test with configuration.

Usage:

Usage of ./scrapercli:
  -c string
        config file
  -e string
        entry point (key of the config)
  -hide
        hide the scraper debug info
  -q string
        query string
  -testall
        test all keys in the config using the query

Examples:

./scrapercli -c scraper-config.json -e "google" -q "query=black&page=0"

./scrapercli -c scraper-config.json -q "query=black&page=0" -testall -hide

Similar projects
MIT License

Copyright © 2016 <dev@jpillora.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.0.1

type Config map[string]*Endpoint

the configuration file

type Endpoint added in v1.0.1

type Endpoint struct {
	Name    string                `json:"name,omitempty"`
	Method  string                `json:"method,omitempty"`
	URL     string                `json:"url"`
	Body    string                `json:"body,omitempty"`
	Headers map[string]string     `json:"headers,omitempty"`
	List    string                `json:"list,omitempty"`
	Result  map[string]Extractors `json:"result"`
	Debug   bool
}

Endpoint represents a single remote endpoint. The performed query can be modified between each call by parameterising URL. See documentation.

func (*Endpoint) Execute added in v1.0.1

func (e *Endpoint) Execute(params map[string]string) ([]Result, error)

Execute will execute an Endpoint with the given params

type Extractor added in v1.0.1

type Extractor struct {
	// contains filtered or unexported fields
}

func MustExtractor added in v1.0.1

func MustExtractor(value string) *Extractor

func NewExtractor added in v1.0.1

func NewExtractor(value string) (*Extractor, error)

func (*Extractor) Set added in v1.0.1

func (e *Extractor) Set(value string) (err error)

sets the current string Value as the Extractor function

type Extractors added in v1.0.1

type Extractors []*Extractor

func (Extractors) MarshalJSON added in v1.0.1

func (ex Extractors) MarshalJSON() ([]byte, error)

func (*Extractors) UnmarshalJSON added in v1.0.1

func (ex *Extractors) UnmarshalJSON(data []byte) error

type Handler added in v1.0.1

type Handler struct {
	Config  Config            `opts:"-"`
	Headers map[string]string `opts:"-"`
	Auth    string            `help:"Basic auth credentials <user>:<pass>"`
	Log     bool              `opts:"-"`
	Debug   bool              `help:"Enable debug output"`
}

func (*Handler) Endpoint added in v1.0.1

func (h *Handler) Endpoint(path string) *Endpoint

Endpoint will return the Handler's Endpoint from its Config

func (*Handler) LoadConfig added in v1.0.1

func (h *Handler) LoadConfig(b []byte) error

func (*Handler) LoadConfigFile added in v1.0.1

func (h *Handler) LoadConfigFile(path string) error

func (*Handler) ServeHTTP added in v1.0.1

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Result added in v1.0.1

type Result map[string]string

Result represents a result

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL