Requests模拟登陆豆瓣FM失败


想用requests抓取一些豆瓣FM上的东西,模拟登陆失败。代码如下:


 #!/usr/bin/python
import requests
from subprocess import Popen
# -- Login Start --
postUrl='http://douban.fm/j/misc/login_form'
hostUrl='http://douban.fm/'
user_agent='Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0'
headers={   'User-Agent':user_agent,
            'Connection':'keep-alive',
        'Referer': hostUrl}
s=requests.Session()
s.get(hostUrl,headers=headers)
#Get userName and password
userName=raw_input('Enter your userName:')
password=raw_input('Enter your password:')
#Get captcha
print 'Get captcha...'
rC_id=s.get('http://douban.fm/j/new_captcha',headers=headers)
captcha_id=rC_id.text.strip('"')
captcha=s.get('http://douban.fm/misc/captcha?size=m&id='+captcha_id,headers=headers)
print 'Writing...'
f=file('captcha.jpg','wb')
f.write(captcha.content)
f.close()
print 'Open picture...'
Popen('captcha.jpg', shell=True)
userInput=raw_input('Enter the captcha:')
#Post loginform
payload={   'source':'radio',
            'alias': userName,
            'form_password': password,
            'captcha_solution': userInput,
            'captcha_id': captcha_id,
            'task': 'sync_channel_list'
        }
r=s.post(postUrl,data=payload,headers=headers)
r.raise_for_status() #check status code
# -- Login End --

如果成功登陆的话,post过去登陆表单之后服务器会返回用户信息。但是现在print r.text出来的是网页。

python requests 豆瓣

Fench 9 years, 7 months ago

我是来歪楼的……

因为网页登陆每次都惨遭验证码,所以还没想过自动登录豆瓣…

会觉得用API靠谱些,你说呢

foxnake answered 9 years, 7 months ago

我感覺你給的那個url, 在網頁上都沒辦法登錄.
嘗試下用 http://douban.com/accounts/login 來模擬登錄, 然後分享session.

更簡單點, 用官方API

豆瓣API V2
豆瓣 API 参考手册

最弱D香蕉 answered 9 years, 7 months ago

Your Answer