下記の問題点を解決する方法をご教授いただけませんでしょうか?
よろしくお願いいたします。
解決したい事
- 元のHTMLファイルの内容が変わってしまいます
-- 内容の一部が消えます
-- 一部の箇所で<が<に、>が>になる場合があります
- 改行、空白、タブが変更されます
コードの内容
php
1from bs4 import BeautifulSoup 2 3path = 'C:/home/sitename/include/file.html' 4 5file_read = open(path, 'r') 6line = file_read.read() 7 8soup = BeautifulSoup(line, 'html.parser') 9new_tag = '<?include file="include/item/`$item.code`.html"?>' 10soup.find(id="hoge_fuga").replace_with(new_tag) 11 12str_html = str(soup.prettify) 13 14file_write = open(path, 'w') 15file_write.write(str_html) 16
HTMLファイル(一部抜粋)
HTML
1<!-****************変更前*********************-> 2<img src="<?img_item item=$data size="100"?>" alt="<?$data.name?>" width="100" height="50" id="hoge_fuga"> 3 4<!-****************変更後*********************-> 5//崩れてしまいます。 6<img 100"?="" src="<?img_item item=$data size="/> 7" alt=" 8<?$data.name?> 9" width="100" height="50" id="hoge_fuga"> 10
環境
- Windeos10
- Spyder(Python3.7)
- HTMLファイルの文字コードはShift_JIS
補足
- ちゃんとしたHTMLでないとパーサーに変更されてしまう?
あなたの回答
tips
プレビュー