gpt4 book ai didi

cookies - 尝试登录 Amazon.com 以提取数据,但得到启用 Cookies 响应(使用 Go)

转载 作者:数据小太阳 更新时间:2023-10-29 03:16:00 26 4
gpt4 key购买 nike

我正在尝试使用 Go 登录我在 Amazon 上的帐户以自动提取一些信息,但我无法登录,因为它提示 cookie。这是我使用的代码的净化版本:

package main

import (
"bytes"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"strconv"
)

func CheckThis(AmazonUsername string, AmazonPassword string) error {

var LogonURL string

// Set the url
LogonURL = "https://www.amazon.com/ap/signin"

// Craft some form data
form := url.Values{}
form.Add("appAction", "SIGNIN")
form.Add("email", AmazonUsername)
form.Add("password", AmazonPassword)
form.Add("create", "0")
form.Add("appActionToken", “$VALUE”)
form.Add("openid.pape.max_auth_age", "$VALUE==")
form.Add("openid.identity", "$VALUE=")
form.Add("openid.assoc_handle", "$VALUE")
form.Add("openid.mode", "$VALUE")
form.Add("openid.ns.pape", "$VALUE==")
form.Add("openid.claimed_id", "$VALUE=")
form.Add("pageId", "$VALUE")
form.Add("openid.ns", "$VALUE=")

// Amazon sells cookies
cookieJar, _ := cookiejar.New(nil)

// Create a new client with the cookiejar in the struct...
client := &http.Client{
Jar: cookieJar,
}

// Craft the request to send to the website with the form containing login info
req, _ := http.NewRequest("POST", LogonURL, bytes.NewBufferString(form.Encode()))

// Some more headers
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode())))
req.Header.Add("Accept-Language", "en-US,en;q=0.8")
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
req.Header.Add("Connection", "keep-alive")
req.Header.Add("Host", "www.amazon.com")
req.Header.Add("Referer", "https://www.amazon.com/ap/signin")
req.Header.Add("Upgrade-Insecure-Requests", "1")
req.Header.Add("Origin", "https://www.amazon.com")
req.Header.Add("Cache-Control", "max-age=0")

// And we're off to the races...
resp, _ := client.Do(req)

// What was in the response?
charResponse, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

// Write response body to a text file with title of…
_ = WriteOutputToFile(string(charResponse), “response.html")

// All done!
return nil
}

$VALUE 条目是因为我不确定这些字符串对我的帐户是否重要,所以我删除了它们;这些是我从 Chrome 登录 session 的开发人员工具中提取的值。为简洁起见,我还删除了错误检查。

回复页面(在 Chrome 中打开我驱动器上的 response.html)如下所示: Amazon Response

为了将 cookie 保留在登录页面和后续页面的客户端请求/响应中,我缺少什么?

或者我是否遗漏了一些东西,我保存的响应页面在我呈现 HTML 时试图从 Amazon 中提取元素,而 cookie 问题是因为当我尝试从中查看结果时浏览器会丢失 cookie 信息Go 应用程序?

最佳答案

我很确定亚马逊每次尝试登录时都会使用不同的数据,以便更好地解析登录表单。这是例子

package main

import (
"bytes"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"net/url"
"github.com/PuerkitoBio/goquery"
"log"
)

func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}

func CheckThis(AmazonUsername string, AmazonPassword string) error {
cookieJar, _ := cookiejar.New(nil)

client := &http.Client{
Jar: cookieJar,
}

res, err := client.Get("https://www.amazon.com/gp/sign-in.html/ref=ord_cart_unrec_signin")
checkError(err)

doc, err := goquery.NewDocumentFromResponse(res)

form := url.Values{}
doc.Find("form[name='signIn'] input").Each(func(i int, s *goquery.Selection) {
name, exist := s.Attr("name")
if exist {
value, exist := s.Attr("value")
if exist {
form.Add(name, value)
}
}

})
form.Set("email", AmazonUsername)
form.Set("password", AmazonPassword)

req, _ := http.NewRequest("POST", "https://www.amazon.com/ap/signin", bytes.NewBufferString(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

res, err = client.Do(req)
checkError(err)
defer res.Body.Close()

charResponse, _ := ioutil.ReadAll(res.Body)

ioutil.WriteFile("response.html", charResponse, 0777)

return nil
}

func main() {
CheckThis("", "")
}

关于cookies - 尝试登录 Amazon.com 以提取数据,但得到启用 Cookies 响应(使用 Go),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35512321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com