Skip to content

Instantly share code, notes, and snippets.

@sfantree
Created April 13, 2020 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfantree/bd4fc31d880f3f20f3acd1eabe9b0007 to your computer and use it in GitHub Desktop.
Save sfantree/bd4fc31d880f3f20f3acd1eabe9b0007 to your computer and use it in GitHub Desktop.
Python cookies with Chrome EditThisCookie
#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
you can find a test web login interface like: https://gist.github.com/mschoebel/9398202
download and install EditThisCookie
https://chrome.google.com/webstore/detail/editthiscookie/fngmhnnpilhplaeedifhccceomclgfbg
click the icon and copy the cookie string
'''
try:
import cookielib
except:
import http.cookiejar as cookielib
# different cookies format
MozillaCookie = '''
// Semicolon separated Cookie File
// This file was generated by EditThisCookie
// Details: http://www.cookiecentral.com/faq/#3.5
// Example: http://www.tutorialspoint.com/javascript/javascript_cookies.htm
Set-Cookie3: session=MTU4Njc2NzM0OXxGZVhSdTlhbWRZRVZjVENiUnZ1MWJYUXFCeEtJWGJPaFVPYUMxR3YtUUE0N2RJVFUyS2ZsWGFIMnJETzhGeWx6cEE9PXyW2nKjfVu1Jg4MyPQsNjNBgsoyBvA_K17iL-HYagzIaQ==; path="/"; domain=127.0.0.1; path_spec; expires="0"; version=0
'''
# you need add '#LWP-Cookies-2.0' first line
LWPCookieJar = '''
#LWP-Cookies-2.0
// Semicolon separated Cookie File
// This file was generated by EditThisCookie
// Details: http://www.cookiecentral.com/faq/#3.5
// Example: http://www.tutorialspoint.com/javascript/javascript_cookies.htm
Set-Cookie3: session=MTU4Njc2NzM0OXxGZVhSdTlhbWRZRVZjVENiUnZ1MWJYUXFCeEtJWGJPaFVPYUMxR3YtUUE0N2RJVFUyS2ZsWGFIMnJETzhGeWx6cEE9PXyW2nKjfVu1Jg4MyPQsNjNBgsoyBvA_K17iL-HYagzIaQ==; path="/"; domain=127.0.0.1; path_spec; expires="0"; version=0
'''
with open('cookies.txt', 'r') as f:
f.read()
cj = cookielib.MozillaCookieJar('cookies.txt')
cj = cookielib.LWPCookieJar('cookies.txt')
# ignore_discard=True is important
cj.load('cookies.txt', ignore_discard=True)
import urllib2
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
print opener.open('https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm').read()
import requests
session = requests.Session()
session.cookies = cookielib.LWPCookieJar('cookies.txt')
session.cookies.load(ignore_discard=True)
print session.get('https://buyertrade.taobao.com/trade/itemlist/list_bought_items.htm').text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment