zoukankan      html  css  js  c++  java
  • Python写的163邮箱的登陆并抓出联系人

      1 #-*- coding:utf-8 -*-
      2 import urllib, urllib2, cookielib
      3 import xml.etree.ElementTree as etree
      4 
      5 class Login163:
      6     header = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}
      7     username = ''
      8     passwd = ''
      9     cookie = None #cookie
     10     cookiefile = './cookie.dat'
     11     user = ''
     12     sid = ''
     13     
     14     def __init__(self, username, passwd):
     15         self.username = username
     16         self.passwd = passwd
     17         #cookie
     18         self.cookie = cookielib.LWPCookieJar()
     19         opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
     20         urllib2.install_opener(opener)
     21     
     22     def login(self):
     23         postdata = {
     24                     'username': self.username,
     25                     'password': self.passwd,
     26                     'type': 1
     27                     }
     28         postdata = urllib.urlencode(postdata)
     29         
     30         #用户登录
     31         req = urllib2.Request(
     32                               url = 'https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2?df=webmail163&from=web&funcid=loginone&iframe=1&language=-1&net=null&passtype=1&product=mail163&race=-2_-2_-2_db&style=-1&uid=' + self.username, 
     33                               data = postdata,#POST请求发送的参数
     34                               headers = self.header#
     35         )
     36         result = urllib2.urlopen(req).read()
     37         result = str(result)
     38         self.user = self.username.split('@')[0]
     39         self.cookie.save(self.cookiefile)#
     40         
     41         if 'sid=' in result:
     42             print ('%s  你已成功登陆163邮箱') %(self.user)
     43             self.sid = result.split('=')[3].split('"')[0]
     44             self.cookie.save(self.cookiefile)
     45             flag = True
     46         else:
     47             flag = False
     48             print '%s 登陆163邮箱失败' %(self.user)
     49         return flag
     50     
     51 #获取通讯录
     52     def address_list(self):
     53         
     54         #请求地址
     55         url = 'http://twebmail.mail.163.com/js4/s?sid='+self.sid+'&func=global:sequential&showAd=false&userType=browser&uid='+self.username
     56         #参数设定(var 变量是必需要的,不然就只能看到:<code>S_OK</code><messages/>这类信息)
     57         #这里参数也是在firebug下查看的。
     58         postdata = {
     59             'func':'global:sequential',
     60             'showAd':'false',
     61             'sid':self.sid,
     62             'uid':self.username,
     63             'userType':'browser',
     64             'var':'<?xml version="1.0"?><object><array name="items"><object><string name="func">pab:searchContacts</string><object name="var"><array name="order"><object><string name="field">FN</string><boolean name="desc">false</boolean><boolean name="ignoreCase">true</boolean></object></array></object></object><object><string name="func">pab:getAllGroups</string></object></array></object>'
     65             }
     66         postdata = urllib.urlencode(postdata)
     67         
     68         #组装请求
     69         req = urllib2.Request(
     70             url = url,
     71             data = postdata,
     72             headers = self.header
     73             )
     74         res = urllib2.urlopen(req).read()
     75         
     76         #解析XML,转换成json
     77         #说明:由于这样请求后163给出的是xml格式的数据,
     78         #为了返回的数据能方便使用最好是转为JSON
     79         json = []
     80         tree = etree.fromstring(res)
     81         obj = None
     82         for child in tree:
     83             if child.tag == 'array':
     84                 obj = child            
     85                 break
     86         #这里多参考一下,etree元素的方法属性等,包括attrib,text,tag,getchildren()等
     87         obj = obj[0].getchildren().pop()
     88         for child in obj:
     89             for x in child:
     90                 attr = x.attrib
     91                 if attr['name']== 'EMAIL;PREF':
     92                     value = {'email':x.text}
     93                     json.append(value)
     94         return json
     95         
     96 #Demo
     97 print("Requesting......\n\n")
     98 login = Login163('XXX@163.com','XXXXXX')
     99 flag = login.login()
    100 if flag is True:
    101     print("Successful landing,Resolved contacts......\n\n")
    102     res = login.address_list()
    103     for x in res:
    104         print(x['email'])
    105 else:
    106     print(flag)

    参考了http://www.cnblogs.com/xiaowuyi/archive/2012/05/21/2511428.html的写法。

    只是在登陆的时候改了一下,

    归根结底,登陆就是要获取sid号,你拥有了sid号,你才能登陆并进行相关操作。

    我的程序在获取sid号方面,跟参考程序有所不同,我是从网页中解析出的sid,

    而参考程序是从cookie中,找出的sid。

    其实都差不多,了解原理就好。

  • 相关阅读:
    本周开发工作时间及内容
    自我介绍
    代码规范
    结对编程
    第二周总结
    20140227WPF学习笔记
    约瑟夫问题
    模式匹配KMP算法
    .NET开发之窗体间的传值转化操作
    北达软TOGAF9鉴定级别认证考试通知 北达软
  • 原文地址:https://www.cnblogs.com/hengli/p/2558402.html
Copyright © 2011-2022 走看看