我如何使用Golang Colly登录亚马逊



我正在尝试登录到我的亚马逊买家帐户获得跟踪信息。我做了wordpress-woocommerce登录和获取信息,但我不能为亚马逊。

package main
import (
"fmt"
"log"
"github.com/gocolly/colly"
)
func main() {
// create a new collector
c := colly.NewCollector()
login_link := "https://www.amazon.de/-/en/ap/signin?openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.de%2F%3Flanguage%3Den_GB%26ref_%3Dnav_signin&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=deflex&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&"
// authenticate
err := c.Post(login_link, map[string]string{"username": "mail@example.com", "password": "123qwerty"})
if err != nil {
log.Fatal(err)
}
// attach callbacks after login
c.OnResponse(func(r *colly.Response) {
log.Println("response received", r.StatusCode) //response received 200
})
c.OnHTML("div", func(h *colly.HTMLElement) {
fmt.Println("PRINT ALL: ", h.Text)
})
// start scraping
c.Visit("https://www.amazon.de/-/en/gp/your-account/order-history?ref_=ya_d_c_yo")
}

Wordpress登录一页-亚马逊登录两页。我们可能需要滚动2页的亚马逊https://i.stack.imgur.com/4TNj5.png→Wordpress Login (One Page)
https://i.stack.imgur.com/bhE4m.png ->Amazon Login(Page #1 - Mail)
https://i.stack.imgur.com/0BFcA.png ->Amazon Login(Page #1 - Password)

在这种情况下,

chromedp是一个非常有用的库。您可以尝试下面的代码片段;

package main
import (
"context"

"os"
"time"
"github.com/chromedp/chromedp"
)
func main() {

var res []byte
ctx, cancel := chromedp.NewContext(context.Background(), chromedp.WithBrowserOption())
defer cancel()
err := chromedp.Run(ctx,
chromedp.Navigate("https://www.amazon.com"),
chromedp.WaitReady("body"),
chromedp.Click(`a[data-nav-role="signin"]`, chromedp.ByQuery),
chromedp.Sleep(time.Second*2),
chromedp.SetValue(`ap_email`, "youramazonemail", chromedp.ByID),
chromedp.Click(`continue`, chromedp.ByID),
chromedp.Sleep(time.Second*1),
chromedp.SetValue(`ap_password`, "youramazonpassword", chromedp.ByID),
chromedp.Click(`signInSubmit`, chromedp.ByID),
chromedp.Sleep(time.Second*2),
chromedp.CaptureScreenshot(&res),
)
if err != nil {
log.Fatal(err)
}
os.WriteFile("loggedin.png", res, 0644)
}

上面给出的示例基本上浏览了登录过程所需的所有步骤。成功登录后,您可以使用上下文(ctx)进行导航,并通过使用相同的函数获取您想要的信息。

chromedp.Run(ctx,
chromedp.Navigate(url),
...)

相关内容

  • 没有找到相关文章

最新更新