python模拟登录163邮箱_python模拟登陆163邮箱并获取通讯录 | 学步园

#-*- coding:UTF-8 -*-import urllib,urllib2,cookielibimport xml.etree.ElementTree as etree #xml解析类class Login163:#伪装browserheader = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}username = ''passwd = ''cookie = None #cookie对象cookiefile = './cookies.dat' #cookie临时存放地user = ''def __init__(self,username,passwd):self.username = usernameself.passwd = passwd#cookie设置self.cookie = cookielib.LWPCookieJar() #自定义cookie存放opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))urllib2.install_opener(opener)#登陆def login(self):#请求参数设置postdata = {'username':self.username,'password':self.passwd,'type':1}postdata = urllib.urlencode(postdata)#发起请求req = urllib2.Request(url='http://reg.163.com/logins.jsp?type=1&product=mail163&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D1',data= postdata,#请求数据headers = self.header #请求头)result = urllib2.urlopen(req).read()result = str(result)self.user = self.username.split('@')[0]self.cookie.save(self.cookiefile)#保存cookieif '登录成功,正在跳转...' in result:#print("%s 你已成功登陆163邮箱。---------\n" %(user))flag = Trueelse:flag = '%s 登陆163邮箱失败。'%(self.user)return flag#获取通讯录def address_list(self):#获取认证sidauth = urllib2.Request(url='http://entry.mail.163.com/coremail/fcg/ntesdoor2?username='+self.user+'&lightweight=1&verifycookie=1&language=-1&style=1',headers = self.header)auth = urllib2.urlopen(auth).read()for i,sid in enumerate(self.cookie):#enumerate()用于同时返数字索引与数值,实际上是一个元组:((0,test[0]),(1,test[1]).......)这有点像php里的foreach 语句的作用sid = str(sid)if 'sid' in sid:sid = sid.split()[1].split('=')[1]breakself.cookie.save(self.cookiefile)#请求地址url = 'http://twebmail.mail.163.com/js4/s?sid='+sid+'&func=global:sequential&showAd=false&userType=browser&uid='+self.username#参数设定(var 变量是必需要的,不然就只能看到:S_OK这类信息)#这里参数也是在firebug下查看的。postdata = {'func':'global:sequential','showAd':'false','sid':sid,'uid':self.username,'userType':'browser','var':'<?xml version="1.0"?>pab:searchContactsFN

name="desc">false

truepab:getAllGroups'}postdata = urllib.urlencode(postdata)#组装请求req = urllib2.Request(url = url,data = postdata,headers = self.header)res = urllib2.urlopen(req).read()#解析XML,转换成json#说明:由于这样请求后163给出的是xml格式的数据,#为了返回的数据能方便使用最好是转为JSONjson = []tree = etree.fromstring(res)obj = Nonefor child in tree:if child.tag == 'array':obj = childbreak#这里多参考一下,etree元素的方法属性等,包括attrib,text,tag,getchildren()等obj = obj[0].getchildren().pop()for child in obj:for x in child:attr = x.attribif attr['name']== 'EMAIL;PREF':value = {'email':x.text}json.append(value)return json#Demoprint("Requesting......\n\n")login = Login163('xxxx@163.com','xxxxx')flag = login.login()if type(flag) is bool:print("Successful landing,Resolved contacts......\n\n")res = login.address_list()for x in res:print(x['email'])else:print(flag)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值