monster's Blog

夕阳碧霄亭

刷豆瓣歌曲数

monster posted @ 2013年1月14日 17:13 in Python , 1716 阅读

学python。。写来练手的。。无技术含量

 

#! /usr/bin/env python
# coding=utf-8
import time
import requests
import Image
check_code_url = 'http://douban.fm/j/new_captcha'
login_url = 'http://douban.fm/j/login'
count_url = 'http://douban.fm/j/mine/playlist'
u_cookie = {'bid':'"mAd/zFn5xZI"'}
check_code_id_package = requests.get(check_code_url, cookies=u_cookie)
if check_code_id_package.status_code != 200:
	print 'cannot connect to server'
	exit()
captcha_id = check_code_id_package.content[1:-1] 
postdata = {'source':'radio', 'task':'sync_channel_list'}
print 'username:'
username = raw_input()
print 'password:'
password = raw_input()
print 'check code:'
img_url = 'http://douban.fm/misc/captcha?size=m&id=' + str(captcha_id)
print 'GET CHECK CODE . . . . . .'
img = requests.get(img_url, cookies=u_cookie)
if img.status_code != 200:
	print 'cannot connect to server'
	exit()
f = open('./check', 'wb')
f.write(img.content)
f.close()
image = Image.open('./check')
#image.show()
print 'OK ...'
check_code = raw_input()
postdata['alias'] = username
postdata['form_password'] = password
postdata['captcha_id'] = captcha_id
postdata['captcha_solution'] = check_code
login = requests.post(login_url, data=postdata, cookies=u_cookie)
if login.status_code != 200:
	print 'cannot connect to server'
	exit()
index = login.headers['set-cookie'].find('dbcl2') + 7
login_cookie = login.headers['set-cookie'][index:]
index = login_cookie.find('"')
login_cookie = login_cookie[0:index]
u_cookie['dbcl2'] = login_cookie
for i in range(1, 10000) :
	get_param = {'type':'e', 'sid':'627691', 'channel':'1001621'}
	requests.get(count_url, params=get_param, cookies=u_cookie)
	msg = 'Have %d Finished' % i
	print msg
	time.sleep(1)

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter