# -*- coding: utf-8 -*-
import requests
import re
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'}
URL = 'https://passport.csdn.net/account/login'
class CSDN(object):
def __init__(self, user, password):
self.user = user
self.password = password
self.s = requests.session()
self.s.headers.update(headers)
self.s.verify = False
def __del__(self):
if self.s:
del self.s
def login(self):
r = self.s.get(URL)
hidden_data = self._get_hidden_fields(r.text)
data = {'username': self.user, 'password': self.password}
data.update(hidden_data)
print data
r = self.s.post(URL, data=data)
print r.status_code
print r.text
@staticmethod
def _get_hidden_fields(response_content):
"""
<input type="hidden" name="lt" value="LT-59480-jVbee0Jst...." />
<input type="hidden" name="execution" value="e4s1" />
<input type="hidden" name="_eventId" value="submit" />
"""
data = {}
pattern_lt = r'name="lt" value="(.*?)"'
m = re.search(pattern_lt, response_content, re.I)
data['lt'] = m.group(1)
pattern_exec = r'name="execution" value="(.*?)"'
m = re.search(pattern_exec, response_content, re.I)
data['execution'] = m.group(1)
pattern_event = r'name="_eventId" value="(.*?)"'
m = re.search(pattern_event, response_content, re.I)
data['_eventId'] = m.group(1)
return data
if __name__ == '__main__':
csdn = CSDN('user', 'password')
csdn.login()
使用 python requests 模拟登陆
最新推荐文章于 2024-03-11 21:11:49 发布