Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "encoding/xml"
- "fmt"
- "io/ioutil"
- "net/http"
- "os"
- "strings"
- )
- type html struct {
- Body body `xml:"body"`
- }
- type body struct {
- Content string `xml:",innerxml"`
- }
- func main() {
- reader := bufio.NewReader(os.Stdin)
- fmt.Println("Go Web Scraper v1.0")
- fmt.Println("---------------------------------------------------------------------------------------------------------")
- fmt.Println("Input a website URL")
- for {
- fmt.Print("$> ")
- input, _ := reader.ReadString('\n')
- input = strings.Replace(input, "\r\n", "", -1)
- if strings.Compare(input, "quit") == 0 {
- break
- } else if strings.HasPrefix(input, "body ") {
- fmt.Println("Body:")
- ShowBody(strings.TrimPrefix(input, "body "))
- break
- } else {
- fmt.Println("Results:")
- ShowPage(input)
- fmt.Println("New location:")
- }
- }
- }
- //ShowPage prints the page to console
- func ShowPage(url string) {
- fmt.Println(string(GetHTTP(url)))
- }
- //ShowBody prints the body to console
- func ShowBody(url string) {
- html := html{}
- xml.Unmarshal(GetHTTP(url), &html)
- fmt.Println(html.Body.Content)
- }
- //GetHTTP sends HTTP Get request and returns the page as a slice of bytes
- func GetHTTP(url string) []byte {
- resp, err := http.Get(url)
- if err != nil {
- fmt.Println(err)
- }
- bytes, _ := ioutil.ReadAll(resp.Body)
- return bytes
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement