python urllib2 模拟网站登陆
2024-10-15 16:01:13
python urllib2 模拟网站登陆
1. 可用浏览器先登陆,然后查看网页源码,分析登录表单
2. 使用python urllib2,cookielib 模拟网页登录
import urllib,urllib2,cookielib
#urllib2 支持 http,https def loginWeb(site,user,pwd):
'''
模拟网页登陆,登陆网址,用户名,密码不能为空
登录post form 表单逻辑需要对应登录网站,可以使用火狐浏览器firebug插件查看登陆请求的网址和参数
'''
formValue={'account':user,
'password':pwd,
#这里可以根据网站添加相应的form表单
} #启用cookie自动管理
cj=cookielib.CookieJar()
opender=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
#伪装浏览器访问
opender.addheaders=[('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36')]
#绑定结果
re=opender.open(site,urllib.urlencode(formValue))
print re.getcode()
print re.read() #这里可以把抓取的网页内容,做 解析,判断是否登陆成功的逻辑 #登陆成功之后的带着cookie的页面访问
pc='https://console.oray.com'#比如个人中心页面
pcre=opender.open(pc)
print pcre.getcode()
print pcre.read() if __name__ == '__main__': site='https://console.oray.com/passport/login'
user='uname'
password='upwd'
loginWeb(site, user, password)
https://gist.github.com/kennethreitz/973705
最新文章
- C# SaveFileDialog的用法(转载)
- Bzoj1176 [Balkan2007]Mokia
- jQuery层次选择器
- CSS自定义弹出框
- 转载WPF SDK研究 之 AppModel
- 删除github账号的方法
- Windows获取其他进程中Edit控件的内容
- IOS引导页的编写
- Order&;Shipping Transactions Status Summary
- “崩溃了?不可能,我全 Catch 住了” | Java 异常处理
- NOIP2018Day1T1 铺设道路
- 虚拟机设置固定ip可以使shell远程连接到服务器
- javascript中正则动态替换为对象中的相应数据
- @ConfigurationProperties 配置详解
- iOS开发下载文件速度计算
- Google Colab 免费GPU服务器使用教程
- 51Nod—1174 区间中最大的数 线段树模版
- MYSQL基础语法的使用
- .net平台性能很不错的轻型ORM类Dapper
- HTML5 借助http请求发送formdata对象,从而上传文件 XMLHttpRequest, FormData