[关闭]
@FadeTrack 2018-07-31T09:19:03.000000Z 字数 2475 阅读 7285

python 3.x 163邮箱登陆,邮件读取

Python


  1. import urllib.request
  2. import urllib.parse
  3. import http.cookiejar,re
  4. opener = None
  5. # 带Cookie访问
  6. def openurl(parms):
  7. global opener
  8. if opener == None:
  9. #cookie设置
  10. cj = http.cookiejar.CookieJar()
  11. opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
  12. ret = opener.open(parms)
  13. return ret
  14. def login_163(**parms):
  15. #初始化
  16. parms_key = ['domain','password','username']
  17. arg = {}
  18. for key in parms_key:
  19. if key in parms:
  20. arg[key] = parms[key]
  21. else:
  22. arg[key] = ''
  23. #获取syscheckcode
  24. pre_login = arg['domain']
  25. html = openurl(pre_login).read().decode('utf-8')
  26. patt = re.compile(r'.*?name=syscheckcode.*?value="(.*?)".*?')
  27. syscheckcode = patt.search(html)
  28. if not syscheckcode:
  29. raise Exception('GET syscheckcode Fail!')
  30. syscheckcode = syscheckcode.group(1)
  31. #登陆
  32. postdata = {
  33. 'syscheckcode':syscheckcode,
  34. 'password':arg['password'],
  35. 'username':arg['username'],
  36. }
  37. postdata = urllib.parse.urlencode(postdata)
  38. postdata = postdata.encode('utf-8')
  39. req = urllib.request.Request(
  40. url= arg['domain'],
  41. data=postdata
  42. )
  43. html = openurl(req).read().decode('utf-8')
  44. thisurl = 'http://reg.163.com/Main.jsp?username=' + arg['username']
  45. html = openurl(thisurl).read().decode('utf-8')
  46. # 获取随机key
  47. thisurl = 'http://entry.mail.163.com/coremail/fcg/ntesdoor2?verifycookie=1&lightweight=1&from=urs'
  48. html = openurl(thisurl).read().decode('utf-8')
  49. patt = re.compile(r'.*?@163.com&sid=(.*?)&from.*?')
  50. sid = patt.search(html);
  51. sid = sid.group(1)
  52. # 获取sid
  53. thisurl = 'http://mail.163.com/js6/main.jsp?sid=' + sid
  54. html = openurl(thisurl).read().decode('utf-8')
  55. thisurl = 'http://mail.163.com/js6/s?sid=' + sid + '&func=mbox:listMessages&topNav_mobileIcon_show=1&TopTabReaderShow=1&TopTabLofterShow=1'
  56. # 获取邮件key --- 可以读取看看,实际上是一个类似xml的表,所有的邮件都在这里,我们需要的是key,这里是抽取的第一封邮件的key
  57. html = openurl(thisurl).read().decode('utf-8')
  58. patt = re.compile(r'.*?name="id">(.*?)</string>.*?')
  59. key = patt.search(html);
  60. key = key.group(1)
  61. # 获取邮件内容
  62. thisurl = 'http://mail.163.com/js6/read/readhtml.jsp?mid=' + key
  63. html = openurl(thisurl).read().decode('utf-8')
  64. # 测试输出
  65. print(html)
  66. # 假设返回假,,这个验证可以最后加上
  67. flag = True
  68. #if 'succeedhandle_login' in html:
  69. #flag = True
  70. return flag
  71. # 这里是开始,我懒得缩进了 if __name__ == '__main__':
  72. # 用户名 及 密码
  73. while True:
  74. user = input('input your username:')
  75. pwd = input('input your password:')
  76. if len(user) != 0 and len(pwd) != 0:
  77. break
  78. print('输入错误')
  79. # 测试网站
  80. dom='https://reg.163.com/logins.jsp'
  81. try:
  82. flag = login_163(username=user,password=pwd,domain=dom)
  83. if not flag:
  84. print('读取失败!')
  85. exit(0)
  86. else:
  87. print('读取成功')
  88. except Exception as e:
  89. print('Error:',e)

反正大致过程就是上面那样,,,很标准的 post登陆,之后 163 的页面比较特殊,具体可以自己去试试。

那个key页面是抓包之后找到的,通过那个key就能获得每一封邮件了。

整个代码是从一份功能代码中抽出来,因为剩下的内容涉及xxx,所以不发了。

本代码为自己所写,自己抓包测的,没有任何参考, 如有雷同,绝逼是被抄袭。

标签(空格分隔): 未分类


在此输入正文

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注