drhwb470572 2019-06-20 20:48
浏览 218
已采纳

由于字符无效,无法将cookie加载到cookiejar中

I'm trying to add a cookie to persistent storage and retrieve it in order to parse a site that needs to be logged into.

I'm getting my cookie from an extension and adding it into my cookiejar using a juju cookiejar that reads the default cookie file from env vars but I keep getting an error cannot load cookies: invalid character 'c' looking for beginning of value c-representing the first character in the txt file. I'm wondering if I'm parsing this correctly or not.

<!-- language: lang-go -->
func main(){
    jujujar, err := cookiejar.New(&cookiejar.Options{
    Filename: cookiejar.DefaultCookieFile(),
})

if err != nil {
    panic(err)
}

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

response, err := client.Get("https://example.com/categories/ProductList.aspx?Category=someCategories")

if err != nil {
    panic(err)
}

query, err := goquery.NewDocumentFromResponse(response)
if err != nil {
    panic(err)
}

myQuery := query.Find("body a").Each(func(index int, item *goquery.Selection) {
    linkTag := item
    link, _ := linkTag.Attr("href")
    linkText := linkTag.Text()
    fmt.Printf("Link #%d: '%s' - '%s'
", index, linkText, link)
})

fmt.Print(myQuery)

}

Update, it looks like the library is looking for Json data:

// mergeFrom reads all the cookies from r and stores them in the Jar.
func (j *Jar) mergeFrom(r io.Reader) error {
decoder := json.NewDecoder(r)
// Cope with old cookiejar format by just discarding
// cookies, but still return an error if it's invalid JSON.
var data json.RawMessage
if err := decoder.Decode(&data); err != nil {
    if err == io.EOF {
        // Empty file.
        return nil
    }
    return err
}
var entries []entry
if err := json.Unmarshal(data, &entries); err != nil {
    log.Printf("warning: discarding cookies in invalid format (error: %v)", err)
    return nil
}
j.merge(entries)
return nil
}
  • 写回答

1条回答 默认 最新

  • dqitk20644 2019-06-20 22:22
    关注

    Juju expects the cookies to be saved in JSON format:

    https://github.com/juju/persistent-cookiejar/blob/master/serialize.go

    The JSON needs to deserialize to []entry as defined here:

    https://github.com/juju/persistent-cookiejar/blob/master/jar.go#L140

    The plugin seems to save cookies in this format:

    [domain] / [true or false] / [true or false] / [Epoch date/time] / [name] / [content]

    Not sure what the true or false is, but you basically need to parse those lines and map them to your own Entry struct (as theirs is not exported) - you might be able to use a CSV parser with a custom delimiter, then serialize it to JSON, then use the JSON you created to pass to Juju.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作