mirror of
https://github.com/Kr1s77/awesome-python-login-model.git
synced 2026-10-16 20:20:42 +08:00
豆瓣登陆修复
This commit is contained in:
+48
-94
@@ -1,107 +1,61 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
Required
|
||||
- requests (必须)
|
||||
- bs4 (必选)
|
||||
- pillow (可选)
|
||||
'''
|
||||
|
||||
from urllib.request import urlretrieve
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from os import remove
|
||||
|
||||
try:
|
||||
import cookielib
|
||||
except:
|
||||
import http.cookiejar as cookielib
|
||||
try:
|
||||
from PIL import Image
|
||||
except:
|
||||
pass
|
||||
"""
|
||||
info:
|
||||
author:CriseLYJ
|
||||
github:https://github.com/CriseLYJ/
|
||||
update_time:2019-04-04
|
||||
"""
|
||||
|
||||
url = 'https://accounts.douban.com/login'
|
||||
|
||||
datas = {'source': 'index_nav',
|
||||
'remember': 'on'}
|
||||
|
||||
headers = {'Host': 'www.douban.com',
|
||||
'Referer': 'https://www.douban.com/',
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
|
||||
'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
|
||||
'Accept-Encoding': 'gzip, deflate, br'}
|
||||
|
||||
# 尝试使用cookie信息
|
||||
session = requests.session()
|
||||
session.cookies = cookielib.LWPCookieJar(filename='cookies')
|
||||
try:
|
||||
session.cookies.load(ignore_discard=True)
|
||||
except:
|
||||
print("Cookies未能加载")
|
||||
# cookies加载不成功,则输入账号密码信息
|
||||
datas['form_email'] = input('Please input your account:')
|
||||
datas['form_password'] = input('Please input your password:')
|
||||
"""
|
||||
模拟登陆豆瓣
|
||||
"""
|
||||
|
||||
|
||||
def get_captcha():
|
||||
'''
|
||||
获取验证码及其ID
|
||||
'''
|
||||
r = requests.post(url, data=datas, headers=headers)
|
||||
page = r.text
|
||||
soup = BeautifulSoup(page, "html.parser")
|
||||
# 利用bs4获得验证码图片地址
|
||||
img_src = soup.find('img', {'id': 'captcha_image'}).get('src')
|
||||
urlretrieve(img_src, 'captcha.jpg')
|
||||
try:
|
||||
im = Image.open('captcha.jpg')
|
||||
im.show()
|
||||
im.close()
|
||||
except:
|
||||
print('到本地目录打开captcha.jpg获取验证码')
|
||||
finally:
|
||||
captcha = input('please input the captcha:')
|
||||
remove('captcha.jpg')
|
||||
captcha_id = soup.find(
|
||||
'input', {'type': 'hidden', 'name': 'captcha-id'}).get('value')
|
||||
return captcha, captcha_id
|
||||
class DouBanLogin(object):
|
||||
def __init__(self, account, password):
|
||||
self.url = "https://accounts.douban.com/j/mobile/login/basic"
|
||||
self.headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
|
||||
}
|
||||
"""初始化数据"""
|
||||
self.data = {
|
||||
"ck": "",
|
||||
"name": account,
|
||||
"password": password,
|
||||
"remember": "true",
|
||||
"ticket": ""
|
||||
}
|
||||
self.session = requests.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("恭喜你,登陆成功")
|
||||
|
||||
def isLogin():
|
||||
'''
|
||||
通过查看用户个人账户信息来判断是否已经登录
|
||||
'''
|
||||
url = "https://www.douban.com/accounts/"
|
||||
login_code = session.get(url, headers=headers,
|
||||
allow_redirects=False).status_code
|
||||
if login_code == 200:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
def get_user_data(self):
|
||||
"""获取用户数据表明登陆成功"""
|
||||
# TODO: 这里填写你用户主页的url
|
||||
url = "这里填写你用户主页的url"
|
||||
# 获取用户信息页面
|
||||
html = self.session.get(url).text
|
||||
print(html)
|
||||
|
||||
|
||||
def login():
|
||||
captcha, captcha_id = get_captcha()
|
||||
# 增加表数据
|
||||
datas['captcha-solution'] = captcha
|
||||
datas['captcha-id'] = captcha_id
|
||||
login_page = session.post(url, data=datas, headers=headers)
|
||||
page = login_page.text
|
||||
soup = BeautifulSoup(page, "html.parser")
|
||||
result = soup.findAll('div', attrs={'class': 'title'})
|
||||
# 进入豆瓣登陆后页面,打印热门内容
|
||||
for item in result:
|
||||
print(item.find('a').get_text())
|
||||
# 保存 cookies 到文件,
|
||||
# 下次可以使用 cookie 直接登录,不需要输入账号和密码
|
||||
session.cookies.save()
|
||||
def run(self):
|
||||
"""运行程序"""
|
||||
self.get_cookie()
|
||||
self.get_user_data()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if isLogin():
|
||||
print('Login successfully')
|
||||
else:
|
||||
login()
|
||||
account = input("请输入你的账号:")
|
||||
password = input("请输入你的密码:")
|
||||
login = DouBanLogin(account, password)
|
||||
login.run()
|
||||
|
||||
@@ -9,7 +9,7 @@ url = "https://movie.douban.com/j/search_subjects"
|
||||
|
||||
# 定义请求头
|
||||
headers = {
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
|
||||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
|
||||
}
|
||||
# 循环构建请求参数并且发送请求
|
||||
for page_start in range(0, 100, 20):
|
||||
|
||||
+6
-13
@@ -6,26 +6,19 @@ import pyquery
|
||||
|
||||
|
||||
def login(session, email, password):
|
||||
'''
|
||||
Attempt to login to Facebook. Returns user ID, xs token and
|
||||
fb_dtsg token. All 3 are required to make requests to
|
||||
Facebook endpoints as a logged in user. Returns False if
|
||||
login failed.
|
||||
'''
|
||||
|
||||
# Navigate to Facebook's homepage to load Facebook's cookies.
|
||||
"""
|
||||
获取cookie
|
||||
"""
|
||||
response = session.get('https://m.facebook.com')
|
||||
|
||||
# Attempt to login to Facebook
|
||||
# 尝试登陆
|
||||
response = session.post('https://m.facebook.com/login.php', data={
|
||||
'email': email,
|
||||
'pass': password
|
||||
}, allow_redirects=False)
|
||||
|
||||
# If c_user cookie is present, login was successful
|
||||
if 'c_user' in response.cookies:
|
||||
|
||||
# Make a request to homepage to get fb_dtsg token
|
||||
# 说明登陆成功
|
||||
homepage_resp = session.get('https://m.facebook.com/home.php')
|
||||
|
||||
dom = pyquery.PyQuery(homepage_resp.text.encode('utf8'))
|
||||
@@ -46,7 +39,7 @@ if __name__ == "__main__":
|
||||
|
||||
session = requests.session()
|
||||
session.headers.update({
|
||||
'User-Agent': 'Mozilla/5.0 (X11; Linux i686; rv:39.0) Gecko/20100101 Firefox/39.0'
|
||||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
|
||||
})
|
||||
|
||||
fb_dtsg, user_id, xs = login(session, args.email, args.password)
|
||||
|
||||
@@ -16,7 +16,7 @@ update_time:2019-3-6
|
||||
|
||||
session = requests.session()
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 5.1; rv:33.0) Gecko/20100101 Firefox/33.0'
|
||||
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
|
||||
}
|
||||
|
||||
QRImgPath = os.path.split(os.path.realpath(__file__))[0] + os.sep + 'webWeixinQr.jpg'
|
||||
|
||||
Reference in New Issue
Block a user