2

我正在尝试登录我的亚马逊买家帐户以获取跟踪信息。我做了 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 登录(一页)
https://i.stack.imgur.com/bhE4m.png -> 亚马逊登录(第 1 页 - 邮件)
https://i.stack.imgur.com/0BFcA.png -> 亚马逊登录(第 1 页 - 密码)

4

1 回答 1

2

在这种情况下, 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),
        ...)
于 2021-09-20T10:43:42.493 回答