Compare commits

..

No commits in common. "30c2a23ad5945b239832620056b409d4a0f250cf" and "87f0cd19408da424e734a561b4713603eae1baae" have entirely different histories.

5 changed files with 16 additions and 47 deletions

View File

@ -1,6 +1,7 @@
package googlesearch
import (
"context"
"fmt"
"strings"
)
@ -11,46 +12,14 @@ func ExampleSearch() {
CountryCode: "au",
}
//lint:ignore SA1012 ignore this bare essentials by passing nil for context and removing context package (despite not being idiomatic go).
serp, err := Search(nil, "First Aid Course Australia Wide First Aid", opt)
serp, err := Search(context.Background(), "First Aid Course Australia Wide First Aid", opt)
if err != nil {
fmt.Print(err.Error())
}
for _, result := range serp {
if strings.Contains(result.URL, "australiawidefirstaid.com.au") {
fmt.Println("Australia Wide First Aid (https://www.australiawidefirstaid.com.au/) found in the serp")
break
}
}
// Output: Australia Wide First Aid (https://www.australiawidefirstaid.com.au/) found in the serp
}
/*
Example of how to set the useragent
*/
func ExampleUserAgent() {
// whatismybrowser.com maintains a database of UserAgents
// https://www.whatismybrowser.com/guides/the-latest-user-agent/chrome
opt := SearchOptions{
CountryCode: "au",
UserAgent: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
}
//lint:ignore SA1012 ignore this bare essentials by passing nil for context and removing context package (despite not being idiomatic go).
serp, err := Search(nil, "First Aid Course Australia Wide First Aid", opt)
if err != nil {
fmt.Print(err.Error())
}
for _, result := range serp {
if strings.Contains(result.URL, "australiawidefirstaid.com.au") {
for _, resault := range serp {
if strings.Contains(resault.URL, "australiawidefirstaid.com.au") {
fmt.Println("Australia Wide First Aid (https://www.australiawidefirstaid.com.au/) found in the serp")
break
}

6
go.mod
View File

@ -1,6 +1,6 @@
module github.com/rocketlaunchr/google-search
go 1.16
go 1.20
require (
github.com/gocolly/colly/v2 v2.1.0
@ -13,10 +13,14 @@ require (
github.com/antchfx/htmlquery v1.3.0 // indirect
github.com/antchfx/xmlquery v1.3.15 // indirect
github.com/antchfx/xpath v1.2.4 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/kennygrant/sanitize v1.2.4 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/temoto/robotstxt v1.1.2 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)

3
go.sum
View File

@ -28,7 +28,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/gocolly/colly v1.2.0 h1:qRz9YAn8FIH0qzgNUw+HT9UN7wm1oF9OBAilwEWpyrI=
github.com/gocolly/colly v1.2.0/go.mod h1:Hof5T3ZswNVsOHYmba1u03W65HDWgpV5HifSuueE0EA=
github.com/gocolly/colly/v2 v2.1.0 h1:k0DuZkDoCsx51bKpRJNEmcxcp+W5N8ziuwGaSDuFoGs=
github.com/gocolly/colly/v2 v2.1.0/go.mod h1:I2MuhsLjQ+Ex+IzK3afNS8/1qP3AedHOusRPcRdC5o0=
@ -120,13 +119,11 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.4.0/go.mod h1:9P2UbLfCdcvo3p/nzKvsmas4TnlujnuoV9hGgYzW1lQ=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View File

@ -31,7 +31,6 @@ type Result struct {
}
const stdGoogleBase = "https://www.google."
const defaultAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"
// GoogleDomains represents localized Google homepages. The 2 letter country code is based on ISO 3166-1 alpha-2.
//
@ -285,7 +284,7 @@ func Search(ctx context.Context, searchTerm string, opts ...SearchOptions) ([]Re
}
if opts[0].UserAgent == "" {
c.UserAgent = defaultAgent
c.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672 Safari/537.36"
} else {
c.UserAgent = opts[0].UserAgent
}

View File

@ -1,23 +1,23 @@
// Copyright 2020-21 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package googlesearch_test
package googlesearch
import (
"context"
"testing"
googlesearch "github.com/rocketlaunchr/google-search"
)
var ctx = context.Background()
func TestSearch(t *testing.T) {
q := "Hello World"
opts := googlesearch.SearchOptions{
opts := SearchOptions{
Limit: 20,
}
//lint:ignore SA1012 ignore this bare essentials by passing nil for context and removing context package (despite not being idiomatic go).
returnLinks, err := googlesearch.Search(nil, q, opts)
returnLinks, err := Search(ctx, q, opts)
if err != nil {
t.Errorf("something went wrong: %v", err)
return