不需要user-agent偽裝就可以登陸成功玻粪,測(cè)試時(shí)間:2017-11-04
# -*- coding: utf-8 -*-
import requests
import re
from bs4 import BeautifulSoup
'''
新建一個(gè)Session實(shí)例脆贵,發(fā)送get請(qǐng)求神僵,判斷是否有captcha
'''
s = requests.Session()
url_login = 'https://accounts.douban.com/login'
response = s.get(url_login)
# print response.request.headers
soup = BeautifulSoup(response.text, 'html.parser')
captcha = soup.find('img', id='captcha_image')
if captcha:
captcha_url = captcha['src']
print captcha_url
captcha_text = raw_input('Please input the captcha:')
re_captcha_id = r'<input type="hidden" name="captcha-id" value="(.*?)"/'
captcha_id = re.findall(re_captcha_id, response.text)
form_data = {
'redir': 'https://www.douban.com/people/48373115/',
'form_email': '15861591989',
'form_password': 'cloud523',
'captcha-solution': captcha_text,
'captcha-id': captcha_id,
# 'login': u'登陸'
}
r = s.post(url_login, data=form_data)
with open("test.html", 'w') as fh:
fh.write(r.text.encode("utf-8"))
fh.close
else:
form_data = {
'redir': 'https://www.douban.com/people/48373115/',
'form_email': '15861591989',
'form_password': 'cloud523',
}
r = s.post(url_login, data=form_data)
with open("test.html", 'w') as fh:
fh.write(r.text.encode("utf-8"))
fh.close