Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2886915
  • 博文数量: 348
  • 博客积分: 2907
  • 博客等级: 中校
  • 技术积分: 2272
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-12 09:16
个人简介

专注 K8S研究

文章分类

全部博文(348)

文章存档

2019年(22)

2018年(57)

2016年(2)

2015年(27)

2014年(33)

2013年(190)

2011年(3)

2010年(14)

分类: Python/Ruby

2013-08-01 14:09:40


  1. #-*- coding:UTF-8 -*-
  2. import urllib,urllib2,cookielib
  3. import xml.etree.ElementTree as etree #xml解析类

  4. class Login163:
  5.    #伪装browser
  6.     header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
  7.     username = ''
  8.     passwd = ''
  9.     cookie = None #cookie对象
  10.     cookiefile = './cookies.dat' #cookie临时存放地
  11.     user = ''
  12.     
  13.     def __init__(self,username,passwd):
  14.         self.username = username
  15.         self.passwd = passwd
  16.         #cookie设置
  17.         self.cookie = cookielib.LWPCookieJar() #自定义cookie存放
  18.         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
  19.         urllib2.install_opener(opener)

  20.    #登陆
  21.     def login(self):

  22.         #请求参数设置
  23.         postdata = {
  24.             'username':self.username,
  25.             'password':self.passwd,
  26.             'type':1
  27.             }
  28.         postdata = urllib.urlencode(postdata)

  29.         #发起请求
  30.         req = urllib2.Request(
  31.                 url='%3D1%26verifycookie%3D1%26language%3D-1%26style%3D1',
  32.                 data= postdata,#请求数据
  33.                 headers = self.header #请求头
  34.             )

  35.         result = urllib2.urlopen(req).read()
  36.         result = str(result)
  37.         self.user = self.username.split('@')[0]

  38.         self.cookie.save(self.cookiefile)#保存cookie
  39.         
  40.         if '登录成功,正在跳转...' in result:
  41.             #print("%s 你已成功登陆163邮箱。---------\n" %(user))
  42.             flag = True
  43.         else:
  44.             flag = '%s 登陆163邮箱失败。'%(self.user)
  45.            
  46.         return flag

  47.    #获取通讯录
  48.     def address_list(self):

  49.         #获取认证sid
  50.         auth = urllib2.Request(
  51.                 url=''+self.user+'&lightweight=1&verifycookie=1&language=-1&style=1',
  52.                 headers = self.header
  53.             )
  54.         auth = urllib2.urlopen(auth).read()
  55.         for i,sid in enumerate(self.cookie):#enumerate()用于同时返数字索引与数值,实际上是一个元组:((0,test[0]),(1,test[1]).......)这有点像php里的foreach 语句的作用
  56.             sid = str(sid)
  57.             if 'sid' in sid:
  58.                 sid = sid.split()[1].split('=')[1]
  59.                 break
  60.         self.cookie.save(self.cookiefile)
  61.         
  62.         #请求地址
  63.         url = ''+sid+'&func=global:sequential&showAd=false&userType=browser&uid='+self.username
  64.         #参数设定(var 变量是必需要的,不然就只能看到:<code>S_OK</code><messages/>这类信息)
  65.         #这里参数也是在firebug下查看的。
  66.         postdata = {
  67.             'func':'global:sequential',
  68.             'showAd':'false',
  69.             'sid':sid,
  70.             'uid':self.username,
  71.             'userType':'browser',
  72.             'var':'pab:searchContactsFNfalsetruepab:getAllGroups'
  73.             }
  74.         postdata = urllib.urlencode(postdata)
  75.         
  76.         #组装请求
  77.         req = urllib2.Request(
  78.             url = url,
  79.             data = postdata,
  80.             headers = self.header
  81.             )
  82.         res = urllib2.urlopen(req).read()
  83.         
  84.         #解析XML,转换成json
  85.         #说明:由于这样请求后163给出的是xml格式的数据,
  86.         #为了返回的数据能方便使用最好是转为JSON
  87.         json = []
  88.         tree = etree.fromstring(res)
  89.         obj = None
  90.         for child in tree:
  91.             if child.tag == 'array':
  92.                 obj = child
  93.                 break
  94.         #这里多参考一下,etree元素的方法属性等,包括attrib,text,tag,getchildren()
  95.         obj = obj[0].getchildren().pop()
  96.         for child in obj:
  97.             for x in child:
  98.                 attr = x.attrib
  99.                 if attr['name']== 'EMAIL;PREF':
  100.                     value = {'email':x.text}
  101.                     json.append(value)
  102.         return json
  103.         
  104. #Demo
  105. print("Requesting......\n\n")
  106. login = Login163('xxxx@163.com','xxxxxxx')
  107. flag = login.login()
  108. if type(flag) is bool:
  109.     print("Successful landing,Resolved contacts......\n\n")
  110.     res = login.address_list()
  111.     for x in res:
  112.         print(x['email'])
  113. else:
  114.     print(flag)

=======================================================================================
执行结果:
Requesting......




Successful landing,Resolved contacts......




job@bokecc.com
liudan@cjzg.cn
lucia@fonsview.com
Selena@nfyg.com.cn
baoquan.qu@zqgame.com
xiaobaowang888@163.com
zhu.jj@paidui.cn

阅读(4718) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~