- Notifications
You must be signed in to change notification settings - Fork1
simp-lee/browser
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package provides a high-level API for managing and interacting with browser instances using therod library. It supports features such as browser pooling, headless mode, proxy configuration, cookies, and automatic idle timeout.
To install the package, usego get:
go get -u github.com/simp-lee/browser
Here's a basic example of how to use the package:
package mainimport ("fmt""github.com/simp-lee/browser""time")funcmain() {// Get a browser instance with default optionsb,err:=browser.GetBrowser()iferr!=nil {panic(err)}deferb.Close()// Get a page instance from the browserpage,err:=b.GetPage()iferr!=nil {panic(err)}deferb.PutPage(page)page.MustNavigate("https://example.com")fmt.Println(page.MustInfo().Title)}
You can configure various options for the browser instance:
package mainimport ("github.com/simp-lee/browser""time")funcmain() {// Get a browser instance with custom optionsb,err:=browser.GetBrowser(browser.WithHeadless(false),browser.WithProxy("127.0.0.1:8080"),browser.WithPoolSize(5),browser.WithIdleTimeout(10*time.Minute),)iferr!=nil {panic(err)}deferb.Close()}
You can configure various options for the page instance:
package mainimport ("fmt""github.com/simp-lee/browser")funcmain() {// Get a browser instance with default optionsb,err:=browser.GetBrowser()iferr!=nil {panic(err)}deferb.Close()// Get a page instance with custom optionspage,err:=b.GetPage(browser.WithUserAgent("CustomUserAgent"),browser.WithReferer("https://example.com"),browser.WithViewport(1920,1080,1.0,false),browser.WithExtraHeaders(map[string]string{"X-Custom-Header":"custom_value",}),browser.WithCookies(browser.Cookie{Name:"example_cookie",Value:"cookie_value",Domain:"example.com",}),)iferr!=nil {panic(err)}deferb.PutPage(page)page.MustNavigate("https://example.com")fmt.Println(page.MustInfo().Title)}
You can block image loading on a page by callingb.BlockImageLoading(page) to save bandwidth`:
package mainimport ("fmt""github.com/simp-lee/browser")funcmain() {// Get a browser instance with default optionsb,err:=browser.GetBrowser()iferr!=nil {panic(err)}deferb.Close()// Get a page instance from the browserpage,err:=b.GetPage()iferr!=nil {panic(err)}deferb.PutPage(page)// Block image loading on the pageiferr:=b.BlockImageLoading(page);err!=nil {panic(err)}page.MustNavigate("https://example.com")fmt.Println(page.MustInfo().Title)}
You can retrieve cookies from a page instance using theGetCookies method::
package mainimport ("fmt""github.com/simp-lee/browser")funcmain() {// Get a browser instance with default optionsb,err:=browser.GetBrowser()iferr!=nil {panic(err)}deferb.Close()// Get a page instance from the browserpage,err:=b.GetPage()iferr!=nil {panic(err)}deferb.PutPage(page)// Navigate to a URLpage.MustNavigate("https://example.com")// Get cookies from the pagecookies,err:=b.GetCookies(page)iferr!=nil {panic(err)}// Print the cookiesfor_,cookie:=rangecookies {fmt.Printf("Cookie: %+v\n",cookie)}}
About
A Go library for managing browser instances with rod, supporting pooling, headless mode, proxies, cookies, and auto timeout.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Uh oh!
There was an error while loading.Please reload this page.