From d52bae27edfe17eae4ab3e2eb3cd6a77efd548cc Mon Sep 17 00:00:00 2001 From: rocketlaunchr-cto Date: Sun, 4 Oct 2020 10:25:55 +1100 Subject: [PATCH] - Google DOM changed. Update scraping code. --- search.go | 6 ++---- search_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 search_test.go diff --git a/search.go b/search.go index e73306e..8aea829 100644 --- a/search.go +++ b/search.go @@ -299,15 +299,13 @@ func Search(ctx context.Context, searchTerm string, opts ...SearchOptions) ([]Re item := sel.Eq(i) - rDiv := item.Find("div.r") + rDiv := item.Find("div.rc") linkHref, _ := rDiv.Find("a").Attr("href") linkText := strings.TrimSpace(linkHref) titleText := strings.TrimSpace(rDiv.Find("h3").Text()) - sDiv := item.Find("div.s") - - descText := strings.TrimSpace(sDiv.Find("span.st").Text()) + descText := strings.TrimSpace(rDiv.Find("div > div > span > span").Text()) if linkText != "" && linkText != "#" { result := Result{ diff --git a/search_test.go b/search_test.go new file mode 100644 index 0000000..e6d519f --- /dev/null +++ b/search_test.go @@ -0,0 +1,29 @@ +package googlesearch_test + +import ( + "context" + "testing" + + "github.com/rocketlaunchr/google-search" +) + +var ctx = context.Background() + +func TestSearch(t *testing.T) { + + q := "Hello World" + + opts := googlesearch.SearchOptions{ + Limit: 20, + } + + returnLinks, err := googlesearch.Search(ctx, q, opts) + if err != nil { + t.Errorf("something went wrong: %v", err) + return + } + + if len(returnLinks) == 0 { + t.Errorf("no results returned: %v", returnLinks) + } +}