forked from wkunzhi/Python3-Spider
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
83 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,101 @@ | ||
import requests | ||
import json | ||
import re | ||
import execjs.runtime_names | ||
|
||
|
||
class People: | ||
def __init__(self, username, pwd): | ||
self.username = username | ||
def __init__(self, user, pwd): | ||
""" | ||
初始化 | ||
:param user: 用户名 | ||
:param pwd: 密码 | ||
""" | ||
self.username = user | ||
self.pwd = pwd | ||
self.index_url = 'http://www.renren.com/' | ||
self.ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36' | ||
self.headers = { | ||
'User-Agent': self.ua, | ||
'Host': 'www.renren.com', | ||
} | ||
self.session = requests.session() | ||
print('引擎', execjs.get().name) | ||
self.json_data = '' | ||
|
||
print('【JS引擎】', execjs.get().name) | ||
with open("enc.js", "r", encoding="utf-8") as f: | ||
self.js = execjs.compile(f.read()) | ||
|
||
def to_html(self): | ||
headers = { | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36', | ||
'Host': 'www.renren.com', | ||
} | ||
response = self.session.get(self.index_url, headers=headers) | ||
with open('index.html', 'w', encoding='utf-8') as f: | ||
f.write(response.text) | ||
print('第一次请求返回', response) | ||
def to_index(self): | ||
""" | ||
第一步 - 访问首页 | ||
获取 Cookies | ||
:return: | ||
""" | ||
response = self.session.get('http://www.renren.com/', headers=self.headers) | ||
print('【主页】', response) | ||
|
||
def get_key(self): | ||
""" | ||
第二步 - 获取加密参数 | ||
获取 rkey 以及 密码加密所需参数 | ||
:return: | ||
""" | ||
headers = { | ||
'Referer': 'http://login.renren.com/ajaxproxy.htm', | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36', | ||
'User-Agent': self.ua, | ||
} | ||
url = 'http://login.renren.com/ajax/getEncryptKey' | ||
response = self.session.get(url, headers=headers) | ||
_dict = json.loads(response.text) | ||
print('获取key返回数据', _dict) | ||
return _dict | ||
response = self.session.get('http://login.renren.com/ajax/getEncryptKey', headers=headers) | ||
print('【获取key】', response.text) | ||
return response.text | ||
|
||
def login(self, _dict): | ||
def login(self, key_info): | ||
""" | ||
第三步 - 登录账号 | ||
:param key_info: 第二步获取的参数 | ||
:return: | ||
""" | ||
url = 'http://www.renren.com/ajaxLogin/login?1=1' + self.js.call('getTime') | ||
data = { | ||
'email': self.username, | ||
'icode': '', | ||
'icode': "", | ||
'origURL': 'http://www.renren.com/home', | ||
'domain': 'renren.com', | ||
'key_id': '1', | ||
'captcha_type': 'web_login', | ||
'password': self.get_password(_dict), | ||
'rkey': _dict.get('rkey') | ||
} | ||
headers = { | ||
'Host': 'www.renren.com', | ||
'Origin': 'http://www.renren.com', | ||
'Referer': 'http://www.renren.com/', | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36', | ||
'password': self.get_password(key_info), | ||
'rkey': json.loads(key_info).get('rkey'), | ||
'f': '' | ||
} | ||
print('登录data', data) | ||
print('登录URL', url) | ||
response = self.session.post(url, data=data, headers=headers) | ||
print(response.text) | ||
print('【登录data】', data) | ||
print('【登录URL】', url) | ||
print('【Cookies】', self.session.cookies) | ||
response = self.session.post(url, data=data, headers=self.headers) | ||
print('【返回信息】', response.text) | ||
response = self.session.get('http://www.renren.com/home', headers=self.headers) | ||
print('【登录信息】', re.findall("<title>(.*?)</title>", response.text)) | ||
|
||
def get_password(self, _dict): | ||
return self.js.call('enc', _dict.get('e'), _dict.get('n'), self.pwd) | ||
def get_password(self, key_info): | ||
""" | ||
调用 js 代码生成参数 | ||
:param key_info: | ||
:return: | ||
""" | ||
return self.js.call('enc', key_info, self.pwd) | ||
|
||
def start(self): | ||
self.to_html() | ||
""" | ||
启动 | ||
:return: | ||
""" | ||
self.to_index() | ||
self.login(self.get_key()) | ||
|
||
|
||
if __name__ == '__main__': | ||
username = input('用户名 >>> ') | ||
password = input('密码 >>> ') | ||
""" | ||
启动区域 | ||
""" | ||
username = input('用户名>>> ') | ||
password = input('密码>>> ') | ||
pp = People(username, password) | ||
pp.start() |