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

最新文章

  1. C# SaveFileDialog的用法(转载)
  2. Bzoj1176 [Balkan2007]Mokia
  3. jQuery层次选择器
  4. CSS自定义弹出框
  5. 转载WPF SDK研究 之 AppModel
  6. 删除github账号的方法
  7. Windows获取其他进程中Edit控件的内容
  8. IOS引导页的编写
  9. Order&Shipping Transactions Status Summary
  10. “崩溃了?不可能,我全 Catch 住了” | Java 异常处理
  11. NOIP2018Day1T1 铺设道路
  12. 虚拟机设置固定ip可以使shell远程连接到服务器
  13. javascript中正则动态替换为对象中的相应数据
  14. @ConfigurationProperties 配置详解
  15. iOS开发下载文件速度计算
  16. Google Colab 免费GPU服务器使用教程
  17. 51Nod—1174 区间中最大的数 线段树模版
  18. MYSQL基础语法的使用
  19. .net平台性能很不错的轻型ORM类Dapper
  20. HTML5 借助http请求发送formdata对象,从而上传文件 XMLHttpRequest, FormData

热门文章

  1. dubbo源码之一——xml schema扩展
  2. QT笔记之VS2010 Qt中导入qrc资源文件
  3. Python3基础 用三个双引号 print输出多行文本
  4. Shell脚本编程初体验
  5. FreeSWITCH 1.6在Debian 8上的安装
  6. [UVa1213]Sum of Different Primes(递推,01背包)
  7. struts2——通配符
  8. Zookeeper未授权访问
  9. python进程、线程、协程(转载)
  10. 在SQL Server中 获取日期、日期格式转换