模拟登陆
import requests
import getpass
"""
模拟登陆豆瓣
"""
class DouBanLogin(object):
## 模拟登陆,获取session
def __init__(self, account, password):
self.url = "https://accounts.douban.com/j/mobile/login/basic"
self.headers={
'Cookie': 'bid=UVp6fA0UmiU',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
}
"""初始化数据"""
self.data = {
'ck':'',
'name':account,
'password':password,
'remember':'false',
'ticket':''
}
self.session = requests.session() ## 创建会话实例,获取session,然后就能以登录的方式去做其他事情
def get_cookie(self):
"""模拟登陆获取cookie"""
html = self.session.post(
url=self.url,
headers=self.headers,
data=self.data
).json()
if html["status"] == "success":
print("恭喜你,登陆成功")
else:
print(html["status"])
def get_user_data(self):
"""获取用户数据表明登陆成功"""
# TODO: 这里填写你用户主页的url
url = "https://www.douban.com/people/219115070/"
# 获取用户信息页面
html = self.session.get(url).text
print(html)
# with open('douban1.html','w',encoding='utf-8') as fp:
# fp.write(html)
# fp.close()
def run(self):
"""运行程序"""
self.get_cookie()
self.get_user_data()
import time
'''
时间戳:
秒级:10位
毫秒级:13位
微秒级:16位
'''
def get_time():
now_time = str(int(time.time()*1000))
print('当前时间戳为:',now_time)
return now_time
get_time()
if __name__ == '__main__':
account = input("请输入你的账号:")
password = getpass.getpass("请输入你的密码:")
login = DouBanLogin(account, password)
login.run()