![](/img/trans.png)
[英]Python speed optimization when downloading 1000+ zip files from URL
[英]Python code to filter styles from 1000+ pages
我已經將word文件轉換為html文件,但是有一個問題,MS word自動向頁面添加了一些樣式。
例如
<div align="center"></div>
<p style=""></p>
<table cellpadding="0">
<tr><img src="...."></img></tr>
</table>
我想輸出為
<div></div>
<p></p>
<table>
<tr><img src="...."></img></tr>
</table>
我不想刪除img內聯樣式。
提前致謝
update: if it is very hard to keep img style in the file. please give me the code excluding that part. it is very urgent for me and i cant edit 1000 pages manually
我建議您使用elementtree 。 解析文件,刪除不需要的所有樣式屬性,然后編寫文件。
使用elementtree時,它應該是5眼線。
如果要刪除已知標簽列表的樣式,我認為不必使用完整的HTML解析器。 就像是
expr = r'((?<=<div)|(?<=<p))[ ]+.*?>'
html_text = re.sub(expr,'>',html_text)
效果很好。 當然,您可以使用要替換的標記數組來生成(?<=
如果您有要刪除的樣式標簽列表,則更加簡單。 只需生成一個像
expr = r' (style|align|myStyleTag)=".*?"'
與re.sub。
如果需要動態組合,請使用解析器。
根據OP的評論進行了編輯:
不幸的是,向后查找需要固定大小的表達式,因此<。*或類似名稱將不起作用。 如果沒有固定的標簽列表,最好使用預先存在的框架。
解決這個問題的方法可能是這樣的:
expr = "("
for i in range(1,8): ## or whatever the max/min tag lengths are
expr += "(?<=<[a-zA-Z]{" + str(i) + "})|"
expr = expr[:-1] + ")[ ]+.*?>"
但這是很糟糕的風格。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.