2019-06-20 20:48 阅读 123


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 {

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

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

if err != nil {

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

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)



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
return nil
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dqitk20644 dqitk20644 2019-06-20 22:22

    Juju expects the cookies to be saved in JSON format:


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


    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.

    点赞 评论 复制链接分享