python - Getting clean mail data from Microsft Graph API
问题描述
I have been trying to get clean mail data from Microsoft Graph API into a Django template.
I have tried using
headers={'Prefer': 'outlook.body-content-type="text"'}
and
headers={'Prefer': 'outlook.body-content-type="html"'}
in my python GET
request but both of them result in data with tags in them. I have tried using regex to clean my data but it is not as effective because regex removes some important texts.
Is there a simpler way of getting a cleaner mail data?
解决方案
您需要使用消息的 body 属性来获取其是否为 HTML/Text。您可以使用首选项标题来获取它。当您指定任一标头时,成功的响应将包括相应的 Preference-Applied 标头,
- 对于文本格式请求:Preference-Applied:outlook.body-content-type="text"
- 对于 HTML 格式请求:Preference-Applied:outlook.body-content-type="html"
如果正文是 HTML,默认情况下,Outlook 会在返回 REST 响应中的正文内容之前删除嵌入在正文属性中的任何可能不安全的 HTML(例如 JavaScript)。
- 要获取完整的原始 HTML 内容,请包含“Prefer: outlook.allow-unsafe-html” HTTP 请求标头
- 除了上述之外,您还可以通过 $value 获取消息的 MIME。
- 此外,您最终检索了 HTML 数据,然后使用 Beautiful Soup 清除了标签。
推荐阅读
- netsuite - 从特定的自定义报告创建报告供应商
- python - 为什么这个 SQLite 查询在第一个 sintax 中失败,而在第二个中没有?
- python - 烧瓶 JSONDecodeError:期望值:第 1 行第 1 列(字符 0)
- c++ - 为MPI的每个进程写入文件
- django - 我无法使用 Django Ckeditor 浏览服务器文件
- javascript - JavaScript 内置的 `bind` 的返回值究竟是什么?
- javascript - jquery在单击的元素内显示所有具有更多类的div
- wordpress - 运费未在美国州显示,但在其他国家/地区显示
- python - pandas.interval_range 结束时间小于 Python 开始时间
- javascript - if 语句同时执行 if 和 else