soup

package module
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2022 License: MIT Imports: 12 Imported by: 251

README

soup

Build Status GoDoc Go Report Card

Web Scraper in Go, similar to BeautifulSoup

soup is a small web scraper package for Go, with its interface highly similar to that of BeautifulSoup.

Exported variables and functions implemented till now :

var Headers map[string]string // Set headers as a map of key-value pairs, an alternative to calling Header() individually
var Cookies map[string]string // Set cookies as a map of key-value  pairs, an alternative to calling Cookie() individually
func Get(string) (string,error) {} // Takes the url as an argument, returns HTML string
func GetWithClient(string, *http.Client) {} // Takes the url and a custom HTTP client as arguments, returns HTML string
func Post(string, string, interface{}) (string, error) {} // Takes the url, bodyType, and payload as an argument, returns HTML string
func PostForm(string, url.Values) {} // Takes the url and body. bodyType is set to "application/x-www-form-urlencoded"
func Header(string, string) {} // Takes key,value pair to set as headers for the HTTP request made in Get()
func Cookie(string, string) {} // Takes key, value pair to set as cookies to be sent with the HTTP request in Get()
func HTMLParse(string) Root {} // Takes the HTML string as an argument, returns a pointer to the DOM constructed
func Find([]string) Root {} // Element tag,(attribute key-value pair) as argument, pointer to first occurence returned
func FindAll([]string) []Root {} // Same as Find(), but pointers to all occurrences returned
func FindStrict([]string) Root {} //  Element tag,(attribute key-value pair) as argument, pointer to first occurence returned with exact matching values
func FindAllStrict([]string) []Root {} // Same as FindStrict(), but pointers to all occurrences returned
func FindNextSibling() Root {} // Pointer to the next sibling of the Element in the DOM returned
func FindNextElementSibling() Root {} // Pointer to the next element sibling of the Element in the DOM returned
func FindPrevSibling() Root {} // Pointer to the previous sibling of the Element in the DOM returned
func FindPrevElementSibling() Root {} // Pointer to the previous element sibling of the Element in the DOM returned
func Children() []Root {} // Find all direct children of this DOM element
func Attrs() map[string]string {} // Map returned with all the attributes of the Element as lookup to their respective values
func Text() string {} // Full text inside a non-nested tag returned, first half returned in a nested one
func FullText() string {} // Full text inside a nested/non-nested tag returned
func SetDebug(bool) {} // Sets the debug mode to true or false; false by default
func HTML() {} // HTML returns the HTML code for the specific element

Root is a struct, containing three fields :

  • Pointer containing the pointer to the current html node
  • NodeValue containing the current html node's value, i.e. the tag name for an ElementNode, or the text in case of a TextNode
  • Error containing an error in a struct if one occurrs, else nil is returned. A detailed text explaination of the error can be accessed using the Error() function. A field Type in this struct of type ErrorType will denote the kind of error that took place, which will consist of either of the following
    • ErrUnableToParse
    • ErrElementNotFound
    • ErrNoNextSibling
    • ErrNoPreviousSibling
    • ErrNoNextElementSibling
    • ErrNoPreviousElementSibling
    • ErrCreatingGetRequest
    • ErrInGetRequest
    • ErrReadingResponse

Installation

Install the package using the command

go get github.com/anaskhan96/soup

Example

An example code is given below to scrape the "Comics I Enjoy" part (text and its links) from xkcd.

More Examples

package main

import (
	"fmt"
	"github.com/anaskhan96/soup"
	"os"
)

func main() {
	resp, err := soup.Get("https://xkcd.com")
	if err != nil {
		os.Exit(1)
	}
	doc := soup.HTMLParse(resp)
	links := doc.Find("div", "id", "comicLinks").FindAll("a")
	for _, link := range links {
		fmt.Println(link.Text(), "| Link :", link.Attrs()["href"])
	}
}

Contributions

This package was developed in my free time. However, contributions from everybody in the community are welcome, to make it a better web scraper. If you think there should be a particular feature or function included in the package, feel free to open up a new issue or pull request.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// Headers contains all HTTP headers to send
	Headers = make(map[string]string)

	// Cookies contains all HTTP cookies to send
	Cookies = make(map[string]string)
)

Init a new HTTP client for use when the client doesn't want to use their own.

Functions

func Cookie(n string, v string)

Cookie sets a cookie for http requests

func Get

func Get(url string) (string, error)

Get returns the HTML returned by the url as a string using the default HTTP client

func GetWithClient added in v1.1.1

func GetWithClient(url string, client *http.Client) (string, error)

GetWithClient returns the HTML returned by the url using a provided HTTP client

func Header(n string, v string)

Header sets a new HTTP header

func Post added in v1.2.2

func Post(url string, bodyType string, body interface{}) (string, error)

Post returns the HTML returned by the url as a string using the default HTTP client

func PostForm added in v1.2.2

func PostForm(url string, data url.Values) (string, error)

PostForm is a convenience method for POST requests that

func PostWithClient added in v1.2.2

func PostWithClient(url string, bodyType string, body interface{}, client *http.Client) (string, error)

PostWithClient returns the HTML returned by the url using a provided HTTP client The type of the body must conform to one of the types listed in func getBodyReader()

func SetDebug added in v1.0.1

func SetDebug(d bool)

SetDebug sets the debug status Setting this to true causes the panics to be thrown and logged onto the console. Setting this to false causes the errors to be saved in the Error field in the returned struct.

Types

type Error added in v1.2.1

type Error struct {
	Type ErrorType
	// contains filtered or unexported fields
}

Error allows easier introspection on the type of error returned. If you know you have a Error, you can compare the Type to one of the exported types from this package to see what kind of error it is, then further inspect the Error() method to see if it has more specific details for you, like in the case of a ErrElementNotFound type of error.

func (Error) Error added in v1.2.1

func (se Error) Error() string

type ErrorType added in v1.2.1

type ErrorType int

ErrorType defines types of errors that are possible from soup

const (
	// ErrUnableToParse will be returned when the HTML could not be parsed
	ErrUnableToParse ErrorType = iota
	// ErrElementNotFound will be returned when element was not found
	ErrElementNotFound
	// ErrNoNextSibling will be returned when no next sibling can be found
	ErrNoNextSibling
	// ErrNoPreviousSibling will be returned when no previous sibling can be found
	ErrNoPreviousSibling
	// ErrNoNextElementSibling will be returned when no next element sibling can be found
	ErrNoNextElementSibling
	// ErrNoPreviousElementSibling will be returned when no previous element sibling can be found
	ErrNoPreviousElementSibling
	// ErrCreatingGetRequest will be returned when the get request couldn't be created
	ErrCreatingGetRequest
	// ErrInGetRequest will be returned when there was an error during the get request
	ErrInGetRequest
	// ErrCreatingPostRequest will be returned when the post request couldn't be created
	ErrCreatingPostRequest
	// ErrMarshallingPostRequest will be returned when the body of a post request couldn't be serialized
	ErrMarshallingPostRequest
	// ErrReadingResponse will be returned if there was an error reading the response to our get request
	ErrReadingResponse
)

type Root

type Root struct {
	Pointer   *html.Node
	NodeValue string
	Error     error
}

Root is a structure containing a pointer to an html node, the node value, and an error variable to return an error if one occurred

func HTMLParse

func HTMLParse(s string) Root

HTMLParse parses the HTML returning a start pointer to the DOM

func (Root) Attrs

func (r Root) Attrs() map[string]string

Attrs returns a map containing all attributes

func (Root) Children added in v1.1.1

func (r Root) Children() []Root

Children returns all direct children of this DOME element.

func (Root) Find

func (r Root) Find(args ...string) Root

Find finds the first occurrence of the given tag name, with or without attribute key and value specified, and returns a struct with a pointer to it

func (Root) FindAll

func (r Root) FindAll(args ...string) []Root

FindAll finds all occurrences of the given tag name, with or without key and value specified, and returns an array of structs, each having the respective pointers

func (Root) FindAllStrict added in v1.1.1

func (r Root) FindAllStrict(args ...string) []Root

FindAllStrict finds all occurrences of the given tag name only if all the values of the provided attribute are an exact match

func (Root) FindNextElementSibling

func (r Root) FindNextElementSibling() Root

FindNextElementSibling finds the next element sibling of the pointer in the DOM returning a struct with a pointer to it

func (Root) FindNextSibling

func (r Root) FindNextSibling() Root

FindNextSibling finds the next sibling of the pointer in the DOM returning a struct with a pointer to it

func (Root) FindPrevElementSibling

func (r Root) FindPrevElementSibling() Root

FindPrevElementSibling finds the previous element sibling of the pointer in the DOM returning a struct with a pointer to it

func (Root) FindPrevSibling

func (r Root) FindPrevSibling() Root

FindPrevSibling finds the previous sibling of the pointer in the DOM returning a struct with a pointer to it

func (Root) FindStrict added in v1.1.1

func (r Root) FindStrict(args ...string) Root

FindStrict finds the first occurrence of the given tag name only if all the values of the provided attribute are an exact match

func (Root) FullText added in v1.1.1

func (r Root) FullText() string

FullText returns the string inside even a nested element

func (Root) HTML added in v1.2.2

func (r Root) HTML() string

HTML returns the HTML code for the specific element

func (Root) Text

func (r Root) Text() string

Text returns the string inside a non-nested element

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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