Skip to content

Instantly share code, notes, and snippets.

@scturtle
Created April 22, 2011 10:02
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 scturtle/936383 to your computer and use it in GitHub Desktop.
Save scturtle/936383 to your computer and use it in GitHub Desktop.
show all doubanFM favorite songs
# coding: utf-8
import cookielib,urllib2
from cStringIO import StringIO
from pysqlite2 import dbapi2 as sqlite
from BeautifulSoup import BeautifulSoup
# a useful function from others
def sqlite2cookie(filename,host):
con = sqlite.connect(filename)
con.text_factory = str
cur = con.cursor()
cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies where host like ?"
,['%%%s%%' % host])
ftstr = ["FALSE","TRUE"]
s = StringIO()
s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file! Do not edit.
""")
for item in cur.fetchall():
s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
item[0], ftstr[item[0].startswith('.')], item[1],
ftstr[item[2]], item[3], item[4], item[5]))
s.seek(0)
cookie_jar = cookielib.MozillaCookieJar()
cookie_jar._really_load(s, '', True, True)
return cookie_jar
# get cookie
cookiejar = sqlite2cookie(u'E:\FirefoxPortable\Data\Profile\cookies.sqlite','douban')
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
urllib2.install_opener(opener)
# post cookie
url='http://douban.fm/mine'
req=urllib2.Request(url)
req.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)')
content=urllib2.urlopen(req).read()
# get it
url='http://douban.fm/mine?start=%d&type=liked'
pages=int(raw_input('pages of favs:'))
for i in range(pages):
content=urllib2.urlopen(url % (i*9,)).read()
soup=BeautifulSoup(str(content))
for tr in soup.findAll('tr')[1:]:
td=tr.findAll('td')
print td[0].string,td[1].span.string #,td[1].a.string # title artist album
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment