python邮件读取2

之前的读取有问题所以根据官方API提供的方法重新做了一遍,涉及HTML的解析一并放上

1. 视频讲解

Using Gmail's API To Search & Display Emails Using Python - YouTube

2. 官方文档

Python Quickstart  |  Gmail API  |  Google Developers

3. BeautifulSoup——prettify()

使解析出来的html程序“每逢标签,自动换行”,可以对程序起到很好的辅助作用,如源代码:

html = """  
<html><head><title>The Dormouse's story</title></head>  
<body>  
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>  
<p class="story">Once upon a time there were three little sisters; and their names were  
<a href="http://example.com/elsie" class="sister" id="link1"><!-- Elsie --></a>,  
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and  
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;  
and they lived at the bottom of a well.</p>  
<p class="story">...</p>  
"""  

调用prettify后输出:

<html>
 <head>
  <title>
   The Dormouse's story
  </title>
 </head>
 <body>
  <p class="title" name="dromouse">
   <b>
    The Dormouse's story
   </b>
  </p>
  <p class="story">
   Once upon a time there were three little sisters; and their names were
   <a class="sister" href="http://example.com/elsie" id="link1">
    <!-- Elsie -->
   </a>
   ,
   <a class="sister" href="http://example.com/lacie" id="link2">
    Lacie
   </a>
   and
   <a class="sister" href="http://example.com/tillie" id="link3">
    Tillie
   </a>
   ;  
and they lived at the bottom of a well.
  </p>
  <p class="story">
   ...
  </p>
 </body>
</html>
<p class="title" name="dromouse"><b>The Dormouse's story</b></p>

 4. 去掉换行符

python 过滤掉字符串中的回车符与换行符( ) - 今夜无风 - 博客园 (cnblogs.com)

5. HTML读取table

python Beautiful Soup解析html页面table标签_愤怒的苹果ext的博客-CSDN博客

6. 用正则提取所需信息

使用Python 正则匹配两个特定字符之间的字符方法 - StarZhai - 博客园 (cnblogs.com)

7. 提取A到B之间的内容,包含B

python 正则怎么匹配a到b的内容,包括A和B_百度知道 (baidu.com)

import re
def my_search(text, a, b):
  regexp = r'(%s.*%s)' % (a, b)
  m = re.search(regexp, text)
  if m:
    return m.group(1)
  return ""
 
my_search("123A1234567890B1234""A""B")

 8. CRM需要,添加一部转换

Python之xlsx文件与csv文件相互转换 - 夕阳下的无名草 - 博客园 (cnblogs.com)

9. 邮件标为已读

Python Gmail API Mark Messages as Read | #65 (Gmail API #3) - YouTube

10. test中出现过的问题

主要是一些边缘性的问题,如domain或者realestate没有收到邮件时的处理,只有一封邮件时的处理等。以及一些邮件格式的变化引起的bug,需要多测试并及时总结,找到问题之后对domain+realestate+unknown均修改

更改密码后权限出现了问题,重新下载token后出现问题,解决方法:

gmail api - 403 "Request had insufficient authentication scopes" when creating drafts in Google API with Python - Stack Overflow

原文地址:https://www.cnblogs.com/eleni/p/14439911.html