0
我有2個類似的HTML文件,我們稱它們爲old.html和new.html。使用Python替換2個文件之間的特定塊HTML
我想打開new.html,做一些處理,保存我剛剛編輯的html塊,並替換old.html中的相應塊。
所以,如果new.html樣子:
<html>
<table>
my content
</table>
</html>
而且old.html樣子:
<html>
<!--other html -->
<table>
old content
</table>
<!-- other html -->
之後,old.html看起來像:
<html>
<!--other html -->
<table>
my content
</table>
<!-- other html -->
我認爲我已經解決了這個問題的第一部分,我只是不知道如何實際修改這些文件。 我想,也許在某種程度上使用一些佔位符文本會的工作,但我還是不知道如何從old.html
我有什麼到目前爲止取代的原代碼塊:
from bs4 import BeautifulSoup as Soup
from soupselect import select
new_file = "\\path\\to\\new.html"
old_file = "\\path\\to\\old.html"
f = open(new_file, "rb")
soup = Soup(f)
new_table = soup.table
f2 = open(old_file, "rb")
soup2 = Soup(f2)
old_table = soup2.table
#process new_table here
#how do i replace old_table with new_table?
f.close()
f2.close()
謝謝!值得慶幸的是,我正在使用的文件將保證只包含一個表格,所以這是正常工作。我沒有考慮使用正則表達式。我想我需要像BeautifulSoup這樣的圖書館來完成一些繁重的工作。 – marc 2012-03-16 18:25:28
很高興爲你工作。甜!我第一個接受的答案! – b10hazard 2012-03-16 18:57:09