Giter VIP home page Giter VIP logo

phantomgo's Introduction

phantomgo

a headless browser phantomjs for golang

it is easy to use for you web download, eg: http://www.github.com/k4s/webrowser

import (
	"fmt"
	"io/ioutil"
	"net/http"

	. "github.com/k4s/phantomgo"
)

func main() {
	p := &Param{
		Method: "POST",  //POST or GET ..
		Url:    "http://localhost/go_test/1.php",
		Header:       http.Header{"Cookie": []string{"your cookies"}},
		UsePhantomJS: true,
		PostBody:     "aaa=111",
	}
	brower := NewPhantom()
	resp, err := brower.Download(p)
	if err != nil {
		fmt.Println(err)
	}
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
}

make the phantomjs yourself to the phantomjs javaScript interface:

package main

import (
	"fmt"
	"github.com/k4s/phantomgo"
	"io/ioutil"
)

func main() {
	p := phantomgo.NewPhantom()
	js := `
		var system = require('system');
		console.log(system.args[0],system.args[1],system.args[2]);
		phantom.exit();
		`
	res, _ := p.Exec(js,"11","22")
	output, _ := ioutil.ReadAll(res)
	fmt.Println(string(output))
	
}

a example simulate login by cookies, so you can get web data login after:

package main

import (
	"fmt"
	"github.com/k4s/phantomgo"
	"io/ioutil"
)

func main() {
	p := phantomgo.NewPhantom()

	js := `
var page = require('webpage').create(),
  system = require('system'),
  address;
page.settings.userAgent = 'Mozilla/5.0+(compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)';
phantom.cookiesEnabled = true;

phantom.addCookie({
  'name'     : 'Apache(do yourself)',
  'value'    : '63354989(do yourself)',
  'domain'     :'.weibo.com'});
phantom.addCookie({
  'name'     : 'SINAGLOBAL(do yourself)',
  'value'    : '8156705307(do yourself)',
  'domain'     :'.weibo.com'});
phantom.addCookie({
  'name'     : 'SUB(do yourself)',
  'value'    : '_2A257NF5qDeTxGeNK6VUT8izMzjmIHXVY12Ii(do yourself)',
  'domain'     :'.weibo.com'});
phantom.addCookie({
  'name'     : 'SUBP(do yourself)',
  'value'    : '0033WrSXqPxfM725Ws9jqgMF55529P9D9WhnL77(do yourself)',
  'domain'     :'.weibo.com'});
if (system.args.length === 1) {
  phantom.exit(1);
} else {
  address = system.args[1];
  page.open(address, function (status) {
    console.log(page.content);
    phantom.exit();
  });
}
`
	res, _ := p.Exec(js, "http://weibo.com/55555555/fans?rightmod=1&wvr=6")
	output, _ := ioutil.ReadAll(res)
	fmt.Println(string(output))

}

phantomgo's People

Contributors

k4s avatar soluchok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

phantomgo's Issues

Is it possible to wait for completion Ajax call while loading the page ?

I am trying to download the website which is using javascript & ajax to load some dynamic content using below code snippet.

    p := &phantomgo.Param{
		Method:       req.Settings.Method,
		Url:          url,
		Header:       http.Header{"Content-Type": []string{*contentType}},
		UsePhantomJS: true,
		PostBody:     *body,
		DialTimeout:  2 * time.Minute,
	}
	brower := phantomgo.NewPhantom()
	resp, err := brower.Download(p)
        rawBody, err := ioutil.ReadAll(resp.Body)
	ioutil.WriteFile("index.html", rawBody, 0644)

But the above code is not waiting for ajax call to finish completion of dynamic content.

Is it possible to wait for certain elements to be visible before downloading the HTML?

Please use English

Please provide an English version of the Readme file and translate the comments within the code to English

response should be decoded?

The response after run the phantomJS is JSON .({"Cookie":"","Body":""})So it will need to be decoded by json.Unmarshal() ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.