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

由于字符无效,无法将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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程