Wednesday, December 8, 2010

cookie monster

So had an interesting web scraping issue with systems behind a cookie based "paywall". Turns out you can quickly and simply extract a working cookie file from Chrome's sqlite3 database. There is a whole bunch of stuff scattered across the web that has bits and pieces of how to do this. There are, as with all things a million ways to do anything. So assume you have carefully auth'd to the site using Chrome, you can then go on to delve into the cookie database to use it with curl etc. later on.  There are also ways that you can embed UID/PASSWD inside curl/wget cmd lines. However, plain text on the CLI is always something that gives me the heebie jeebies ;-) Here's what I found (oh and cygwin on windows is totally awesome! Don't try and run windows without it ;-)) First up, the Chrome schema. You can find the database file in:

c:\:Documents\ and\ Settings\user/Local\
Settings/Application\ Data/Google/Chrome/User\ Data/Default/Cookies

PRAGMA table_info() is the sqlite magic to get at this:
$ sqlite3.exe /cygdrive/c/Documents\ and\ Settings/user/Local\ Settings/Application\ Data/Google/Chrome/User\ Data/Default/Cookies
"PRAGMA table_info(cookies)"

Then because of this documented sqlite3 issue over here: and because your cookie file needs to be TSV, you can either use:

1) CTRL-V + TAB to insert an actual tab into the sqlite3.exe -separator '' cli option
2) Or more twisted, plonk a quick sed 's/,/\t/g' on the end of your sqlite3.exe -csv command line option
3) But it turns out a really simple sqlite3.exe -separator $'\t' works just fine

This example just extracts out the cookies for a particular host you are interested in, you can also dump the whole DB by removing the 'like' piece:
$ sqlite3.exe -separator $'\t' /cygdrive/c/Documents\ and\ Settings/user/Local\
Settings/Application\ Data/Google/Chrome/User\ Data/Default/Cookies
"select host_key, 'TRUE','/', 'FALSE', expires_utc, name, value from
cookies where host_key like ''" > cookie.txt

Then you can simply use curl with this new reformatted cookie.txt file to go about your scripted scrapings!
curl -L --cookie ./cookie.txt
QED ;-)

[any opinions here are all mine, and have absolutely nothing to do with my employer]
(c) 2011 James Cuff