获取腾讯企业邮箱的企业通讯录和架构结构

默北 python获取腾讯企业邮箱的企业通讯录和架构结构已关闭评论21,2331字数 2369阅读7分53秒阅读模式

需要先手动登陆腾讯企业邮箱,然后把url中的sid(不是cookie里的)和cookie里的qm_sid、qm_username填到脚本里去,然后运行,会在当前目录下生成两个文件,一个是all_emails.txt,里面是邮箱。
另一个是depart_staff_tree.txt,里面是部门和部门下的人员的树形结构。

#!/usr/bin/env python 
# -*- coding: utf-8 -*-  
 
import requests 
import re 
import sys; 
reload(sys); 
 
sys.setdefaultencoding('utf8'); 
 
  
def print_tree(id, department_infos, level, staff_infors, f): 
  prefix = '----' * level 
  text = prefix + department_infos[id]['name'] + prefix 
  print text 
  f.write(text + '\n') 
  for key, value in department_infos.items(): 
    if value['pid'] == id: 
       
      print_tree(value['id'], department_infos, level+1, staff_infors, f) 
  prefix = '    ' * level   
  for staff in staff_infors: 
    if staff['pid'] == id: 
      text = prefix + staff['name'] + '  ' + staff['alias'] 
      print text 
      f.write(text + '\n') 
 
if __name__ == "__main__": 
  # url参数中的sid 
  sid = 'xxxx' 
  # cookie中的qm_sid 和 qm_username 
  qm_sid = 'xxx' 
  qm_username='xxx' 
 
  all_departments_url = 'http://exmail.qq.com/cgi-bin/laddr_biz?action=show_party_list&sid={sid}&t=contact&view=biz'.format(sid=sid) 
  cookies = dict(qm_sid=qm_sid 
    , qm_username=qm_username) 
  request = requests.get(all_departments_url,cookies=cookies) 
 
  text = request.text 
  regexp = r'{id:"(\S*?)", pid:"(\S*?)", name:"(\S*?)", order:"(\S*?)"}' 
  results = re.findall(regexp, text) 
  department_ids = [] 
  department_infors = dict() 
  root_department = None 
  for item in results: 
     
    department_ids.append(item[0]) 
    department = dict(id=item[0], pid=item[1], name=item[2], order=item[3]) 
     
    department_infors[item[0]] = department 
    if item[1] == 0 or item[1] == '0': 
      root_department = department 
 
  regexp = r'{uin:"(\S*?)",pid:"(\S*?)",name:"(\S*?)",alias:"(\S*?)",sex:"(\S*?)",pos:"(\S*?)",tel:"(\S*?)",birth:"(\S*?)",slave_alias:"(\S*?)",department:"(\S*?)",mobile:"(\S*?)"}' 
  limit = 10000 
 
  all_emails = [] 
  staff_infors = [] 
  for department_id in  department_ids: 
    department_staff_url = 'http://exmail.qq.com/cgi-bin/laddr_biz?t=memtree&limit={limit}&partyid={partyid}&action=show_party&sid={sid}'.format(limit=limit, sid=sid, partyid=department_id) 
     
    request = requests.get(department_staff_url,cookies=cookies) 
     
    text = request.text 
     
    results = re.findall(regexp, text) 
 
     
    for item in results: 
      all_emails.append(item[3]) 
       
      staff = dict(uin=item[0], pid=item[1], name=item[2], alias=item[3], sex=item[4], pos=item[5], tel=item[6], birth=item[7], slave_alias=item[8], department=item[9], mobile=item[10]) 
      staff_infors.append(staff) 
 
  print str(len(all_emails)) + ' emails' 
  with open('all_emails.txt', 'w') as f: 
    for item in all_emails: 
      f.write(item + '\n') 
   
  with open('depart_staff_tree.txt', 'w') as f: 
    print_tree(root_department['id'], department_infors, 0, staff_infors, f)
文章源自运维生存时间-https://www.ttlsa.com/python/fetch-mailaddress-and-corporate-schema-structure/文章源自运维生存时间-https://www.ttlsa.com/python/fetch-mailaddress-and-corporate-schema-structure/
weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 25/05/2016 01:10:26
  • 转载请务必保留本文链接:https://www.ttlsa.com/python/fetch-mailaddress-and-corporate-schema-structure/