Web Scraper in Go, similar to BeautifulSoup
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 :
…
Root is a struct, containing three fields :
Pointer containing the pointer to the current html nodeNodeValue containing the current html node's value, i.e. the tag name for an ElementNode, or the text in case of a TextNodeError 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 followingErrUnableToParseErrElementNotFoundErrNoNextSiblingErrNoPreviousSiblingErrNoNextElementSiblingErrNoPreviousElementSiblingErrCreatingGetRequestErrInGetRequestErrReadingResponseInstall the package using the command
go get github.com/anaskhan96/soup
An example code is given below to scrape the "Comics I Enjoy" part (text and its links) from xkcd.
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"])
}
}
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.
No open issues yet, or sync has not completed.