值得注意的是python提供了3个http库,httplib、urllib和urllib2,能透明处理cookie的是urllib2,想我之前用httplib手动处理cookie,那个痛苦啊。
代码如下:
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
captcha_id = opener.open(urllib2.Request('http://douban.fm/j/new_captcha')).read().strip('"')
captcha = opener.open(urllib2.Request('http://douban.fm/misc/captcha?size=m&id=' + captcha_id)).read())
file = open('captcha.jpg', 'wb')
file = write(captcha)
file.close() 这段代码实现了验证码的下载。
接着,我们填写表单,并提交。
可以看到,登录表单的目标地址为http://douban.fm/j/login,参数有:
source: radio
alias: 用户名
form_password: 密码
captcha_solution: 验证码
captcha_id: 验证码ID
task: sync_channel_list
接下来要做的是用python构造一个表单。
opener.open(
urllib2.Request('http://douban.fm/j/login'),
urllib.urlencode({
'source': 'radio',
'alias': username,
'form_password': password,
'captcha_solution': captcha,
'captcha_id': captcha_id,
'task': 'sync_channel_list'})) 代码整理
结合之前的版本和新增的登录功能,再加上命令行参数处理、频道选择,一个稍稍完善的douban.fm就完成的
View Code
#!/usr/bin/python
# coding: utf-8
import sys
import os
import subprocess
import getopt
import time
import json
import urllib
import urllib2
import getpass
import ConfigParser
from cookielib import CookieJar
# 保存到文件
def save(filename, content):
file = open(filename, 'wb')
file.write(content)
file.close()
# 获取播放列表
def getPlayList(channel='0', opener=None):
url = 'http://douban.fm/j/mine/playlist?type=n&channel=' + channel
if opener == None:
return json.loads(urllib.urlopen(url).read())
else:
return json.loads(opener.open(urllib2.Request(url)).read())
# 发送桌面通知
def notifySend(picture, title, content):
subprocess.call([
'notify-send',
'-i',
os.getcwd() + '/' + picture,
title,
content])
# 登录douban.fm
def login(username, password):
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
while True:
print '正在获取验证码……'
captcha_id = opener.open(urllib2.Request(
'http://douban.fm/j/new_captcha')).read().strip('"')
save(
'验证码.jpg',
opener.open(urllib2.Request(
'http://douban.fm/misc/captcha?size=m&id=' + captcha_id
)).read())
captcha = raw_input('验证码: ')
print '正在登录……'
response = json.loads(opener.open(
urllib2.Request('http://douban.fm/j/login'),
urllib.urlencode({
'source': 'radio',
'alias': username,
'form_password': password,
'captcha_solution': captcha,
'captcha_id': captcha_id,
'task': 'sync_channel_list'})).read())
if 'err_msg' in response.keys():
print response['err_msg']
else:
print '登录成功'
return opener
# 播放douban.fm
def play(channel='0', opener=None):
while True:
if opener == None:
playlist = getPlayList(channel)
else:
playlist = getPlayList(channel, opener)
if playlist['song'] == []:
print '获取播放列表失败'
break
picture,
for song in playlist['song']:
picture = 'picture/' + song['picture'].split('/')[-1]
# 下载专辑封面
save(
picture,
urllib.urlopen(song['picture']).read())
# 发送桌面通知
notifySend(
picture,
song['title'],
song['artist'] + '\n' + song['albumtitle'])
# 播放
player = subprocess.Popen(['mplayer', song['url']])
time.sleep(song['length'])
player.kill()
def main(argv):
# 默认参数
channel = '0'
user = ''
password = ''
# 获取、解析命令行参数
try:
opts, args = getopt.getopt(
argv, 'u:p:c:', ['user=', 'password=', 'channel='])
except getopt.GetoptError as error:
print str(error)
sys.exit(1)
# 命令行参数处理
for opt, arg in opts:
if opt in ('-u', '--user='):
user = arg
elif opt in ('-p', '--password='):
password = arg
elif opt in ('-c', '--channel='):
channel = arg
if user == '':
play(channel)
else:
if password == '':
password = getpass.getpass('密码:')
opener = login(user, password)
play(channel, opener)
if __name__ == '__main__':
main(sys.argv[1:])
查看更多关于python实现douban.fm简易客户端的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did83052