2013-04-17 58 views
2

我想使用python內聯任意網頁。我已經嘗試了幾個庫,但它們都不適用於「真實」的內容,例如有時需要一個複雜的示例。另外,我希望外部樣式表也被內聯。你有什麼建議嗎?這是目前我的測試代碼:是否有任何良好的python css內聯模塊在野外的HTML?

import requests 
import codecs 
from html5tidy import tidy 

url = "http://www.nytimes.com/" 

r = requests.get(url) 
src = tidy(r.text) 


from pypremailer import Premailer 
p = Premailer(src) 
output = p.premail() 
f = open("/tmp/pypremailer.html", "w") 
f.write(output) 
f.close() 

from premailer import transform 
output = transform(src) 
f = open("/tmp/premailer.html", "w") 
f.write(output) 
f.close() 

import pynliner 
output = pynliner.fromString(src) 
f = open("/tmp/pynliner.html", "w") 
f.write(output) 
f.close() 

from inlinestyler.utils import inline_css 
output = inline_css(src) 
f = open("/tmp/inlinestyler.html", "w") 
f.write(output) 
f.close() 

感謝

+0

你究竟想在這裏做什麼? – 2013-09-27 06:16:46

回答

0

Pynliner確實可能最適合網頁的網址,特別是因爲它能夠處理CSS樣式表標籤..

from pynliner import Pynliner 
p = Pynliner() 
p.from_url('http://mashable.com/2014/06/03/iwilllisten-philadelphia/') 
p.run() 

這是理論上..在現實中,你會遇到一些奇怪的BeautifulSoup問題,需要解決..

相關問題