寻找可以接收HTML页面的python代码,并将该页面使用的任何链接的CSS样式定义插入其中-因此不需要任何外部引用的CSS页面。

需要制作单个文件,以作为网站上现有页面的电子邮件附件插入。谢谢你的帮助。

最佳答案

Sven的回答对我有所帮助,但是开箱即用。以下为我做到了:

import bs4   #BeautifulSoup 3 has been replaced
soup = bs4.BeautifulSoup(open("index.html").read())
stylesheets = soup.findAll("link", {"rel": "stylesheet"})
for s in stylesheets:
    t = soup.new_tag('style')
    c = bs4.element.NavigableString(open(s["href"]).read())
    t.insert(0,c)
    t['type'] = 'text/css'
    s.replaceWith(t)
open("output.html", "w").write(str(soup))

关于将CSS整合为HTML的Python代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4246762/

10-12 13:21