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出来的是网页。
Fench
10 years, 2 months ago