Python模拟登录和登录跳转的参考示例
作者:py3study 时间:2023-07-29 07:09:47
# coding:utf-8
import urllib
import urllib2
import cookielib
from bs4 import BeautifulSoup
# 设置登录url
login_url = "******************"
# 创建登录类
class Login(object):
#初始化
def __init__(self):
self.username = ''
self.password = ''
# 验证码
self.rode = ''
#设置cookie
self.cj = cookirlib,LWPCookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)
def setLoginInfo(self, username, passwoed ,rode):
"""设置登录用户信息"""
self.username = username
self.password = password
self.rode = rode
def login(self):
""""模拟登录"""
login_data = {'username': self.username, 'password': self.password, 'rode': self.rode}
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2859.0 Safari/537.36'}
req = urllib2.Request(login_url, data=urllib.urlencode(login_data), headers=headers)
response = urllibs.urlopen(req)
# 加cookie, 自动保存seissionid,做后续跳转
self.operate = self.opener.open(req)
# 通过BeautifulSoup获取网页内容
soup = BeautifulSoup(response, 'lxml')
# 获取网页的content
data = soup.select('#content')
if data:
print u'模拟登录成功!', data
print u'模拟登录失败!'
def skip(self, skip_url):
"""
模拟登录成功后,跳转网页
----------------------------
传递跳转网页的url
"""
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2859.0 Safari/537.36'}
req = urllib2.Request(skip_url, headers=headers)
response = urllib2.urlopen(req)
soup = BeautifulSoup(response, 'lxml')
# 获取跳转后网页的title
print soup.title.string
if __name__ == "__main__":
userlogin = Login()
username = '***********'
password = '***********'
rode = '***********'
# 执行模拟登录方法
userlogin.login()
# 执行模拟登录成功后网页跳转方法
user_login.skip(skip_url = '***************')
来源:https://cloud.tencent.com/developer/article/1568777
标签:python,模拟,登录,跳转
0
投稿
猜你喜欢
vue3.2中的vuex使用详解
2024-05-25 15:16:58
SQL Server AlwaysOn读写分离配置图文教程
2024-01-19 10:20:20
Vue 实现轮播图功能的示例代码
2024-05-11 09:14:15
python实现精准搜索并提取网页核心内容
2021-04-07 09:14:04
你的网站使用了微格式了么
2009-05-21 12:10:00
python cv2截取不规则区域图片实例
2022-08-31 18:58:42
pytest用yaml文件编写测试用例流程详解
2022-04-19 03:55:29
golang 中signal包的Notify用法说明
2023-07-19 19:06:36
关于Python中request发送post请求传递json参数的问题
2022-12-23 06:20:33
php $_SESSION会员登录实例分享
2024-06-05 09:47:58
在Python中进行自动化单元测试的教程
2023-07-16 04:12:30
python三引号输出方法
2023-05-28 13:00:27
.NET Framework SQL Server 数据提供程序连接池
2024-01-27 05:05:23
学习ASP.NET八天入门:第七天
2007-08-07 13:52:00
python人物视频背景替换实现虚拟空间穿梭
2023-06-11 06:58:30
python 提取tuple类型值中json格式的key值方法
2022-10-04 20:05:32
Go http请求排队处理实战示例
2024-05-21 10:26:58
详解Python的文件处理
2022-07-16 23:46:15
Python 字符串处理特殊空格\\xc2\\xa0\\t\\n Non-breaking space
2022-03-09 19:20:16
python实现简单成绩录入系统
2023-09-16 08:00:27