python 实现清水河畔的模拟登陆
2016-05-24 14:25
465 查看
import HTMLParser import urlparse import urllib import urllib2 import cookielib import requests import string import re #登录的主页面 hosturl = 'http://bbs.uestc.edu.cn/member.php?mod=logging&action=login' #post数据接收和处理的页面(我们要向这个页面发送我们构造的Post数据) posturl = 'http://bbs.uestc.edu.cn/member.php?mod=logging&action=login&loginsubmit=yes&loginhash=LdUWe&inajax=1' #设置一个cookie处理器,它负责从服务器下载cookie到本地,并且在发送请求时带上本地的cookie # cj = cookielib.LWPCookieJar() # cookie_support = urllib2.HTTPCookieProcessor(cj) # opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) # urllib2.install_opener(opener) #打开登录主页面(他的目的是从页面下载cookie,这样我们在再送post数据时就有cookie了,否则发送不成功) # h = urllib2.urlopen(hosturl) #构造header,一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。 headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36', 'Referer' : 'http://bbs.uestc.edu.cn/member.php?mod=logging&action=login'} #构造Post数据,他也是从抓大的包里分析得出的。 postData = {'formhash' : 'e04523a3', 'referer' : 'http://bbs.uestc.edu.cn/forum.php', 'loginfield' : 'username', 'username' : '*****',/用户名字 'password' : '*****',/登录密码 'questionid' : '0', 'answer':'', 'loginsubmit':'true', } #需要给Post数据编码 postData = urllib.urlencode(postData) #通过urllib2提供的request方法来向指定Url发送我们构造的数据,并完成登录过程 request=urllib2.Request(posturl, postData, headers) response=urllib2.urlopen(request) print response.read() # print response.read()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法