我正在使用 Gmail API 下载电子邮件。当这些电子邮件是 HTML 时,我尝试使用 Python 的 pdfkit 将它们转换为 PDF。
这在很多情况下都有效,但在某些情况下,html 负载包含图像标签,如 src=“cid:169abdc4ae2c4da871d2”
.
这个“cid”标签似乎是指作为多部分电子邮件的一部分发送的图像,但这不能被 PDFkit 处理。错误是:
wkhtmltopdf reported an error:
Loading pages (1/6)
Error: Failed to load cid:169abf0d0cdfffb7aff2, with network status code 301 and http status code 0 - Protocol "cid" is unknown
我该如何解决这个问题?有没有办法将我从 gmail 有效负载获得的 HTML 转换为具有适当图片源的标准 HTML?
最佳答案
您可以在 w3lib Package 中使用“remove_tags”方法:
删除所有标签:
import w3lib.html
doc = '<div><p><b>This is a link:</b> <a href="http://www.example.com">example</a></p></div>'
w3lib.html.remove_tags(doc)
'This is a link: example'
删除特定标签: w3lib.html.remove_tags(doc, which_ones=('a','b'))
'<div><p>This is a link: example</p></div>'
关于python - 将 Gmail 转换为 PDF : embedded images in HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55317467/