寻找可以获取 HTML 页面并将该页面使用的任何链接的 CSS 样式定义插入其中的 python 代码 - 因此不需要任何外部引用的 css 页面。
需要从网站上使用的现有页面制作单个文件作为电子邮件附件插入。谢谢你的帮助。
您必须自己编写代码,但BeautifulSoup将为您提供很大帮助。假设您的所有文件都是本地的,您可以执行以下操作:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(open("index.html").read())
stylesheets = soup.findAll("link", {"rel": "stylesheet"})
for s in stylesheets:
s.replaceWith('<style type="text/css" media="screen">' +
open(s["href"]).read()) +
'</style>')
open("output.html", "w").write(str(soup))
Run Code Online (Sandbox Code Playgroud)
如果文件不是本地的,您可以使用 Pythonurllib或urllib2来检索它们。