#!/usr/bin/python
#coding:utf-8
#
#2014-2-17
#write:Jack
#info:login in
#
import urllib2, urllib
import cookielib
cj = cookielib.CookieJar()
#声明
connects= urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(connects)
#启用cookie
loginurl = 'http://www.id666.com/cmd/member/user_login.asp?cmd=login'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.2; rv:22.0) Gecko/20100101 Firefox/22.0',
'Referer':'http://www.id666.com/index.asp'
}
postdata = {
'username':'123123',
'password':'123123'
}
postdata = urllib.urlencode(postdata)
#post数据编码转换
#print postdata
openurl = urllib2.Request(loginurl,postdata,headers)
text = urllib2.urlopen(openurl)
#第一次登录,下边是某页面中一个验证码地址,页面就不打开了。直接获取验证码,然后提交到最终地址即可
picurl = 'http://www.id666.com/inc/newcheck.asp'
#验证码
picopen = urllib2.urlopen(picurl)
pictext = picopen.read()
picfile = open('vcode.jpg','wb')
#以二进制写入打开
picfile.write(pictext)
picfile.close()
print '请打开根目录中,图片vcode.jpg,将其值输入下方提示:\n'
num = str(raw_input('输入验证码数值\n'))
postdata = {
'check_code2':num
}
postdata = urllib.urlencode(postdata)
timeurl = urllib2.Request(picurl,postdata)
timeurl = urllib2.urlopen(timeurl)
print timeurl.read()
代码有点小问题,现在忙碌,只能稍后补充了,本想采用PIL进行自动获取验证码,后来试过。效果超级不好!故此,不用