Blue Bot
Blue Bot

Reputation: 2438

Is it possible to get a webpage that requires JS in Golang?

Creating a micro service that gets couple websites main pages html. One of them performs a check for enabled JS and redirects to error page if no JS was detected.

Is there a way around it with Golang?

EDIT: attempted to play with this package (JavaScript interpreter) but with no luck..

EDIT2: its 2020, moved to use js Puppeteer

It uses embedded browser and is a very mature and packed with utilities library. for complex web apps embedded browser is really the only one to go

For backends written in other the js I would still use 'Puppeteer' as a micro service

hope this helps anyone in the future

thanks

Upvotes: 4

Views: 5161

Answers (3)

ranu
ranu

Reputation: 652

Yes, it is possible. Like Gonzalez mentioned earlier, PhantomJS is a good choice. But there are some things I would like to clarify, first there is a problem using the phantomgo repository on Linux as the developer doesn't provide the binary of the PanthomJS for Linux.

The way this repository is used should be using the following instructions:

  1. go get github.com/k4s/phantomgo
  2. go get github.com/k4s/webrowser
  3. Download the pre-compiled binary for your platform of PhantomJS on this page.
  4. Add the binary to the $GOPATH/src/github.com/k4s/phantomgo/phantomgojs folder.
import (
    "fmt"
    "io/ioutil"
    "net/http"

    . "github.com/k4s/webrowser"
)

func main() {
    p := &Param{
        Method:       "GET",
        Url:          "http://google.com",
        Header:       http.Header{"Cookie": []string{"your cookie"}},
        UsePhantomJS: true,
    }
    brower := NewWebrowse()
    resp, err := brower.Download(p)
    if err != nil {
        fmt.Println(err)
    }
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
    fmt.Println(resp.Cookies())
}

Upvotes: 2

Gonzalez
Gonzalez

Reputation: 711

Try PhantomJS for Go https://github.com/k4s/phantomgo

I tried it once and it worked for me, maybe it'll help you.

Upvotes: 1

makhov
makhov

Reputation: 450

Use Headless Chrome. Here is an example.

Upvotes: 0

Related Questions