用Python编辑HTML

时间:2019-06-26 19:18:38

标签: python html parsing beautifulsoup

我正在研究用于翻译HTML中的字符串的代码。

更具体地说,我的目标是执行字符串替换。这些步骤是:文件解析,在一行中标识字符串(如果有),最后用从字典中提取的翻译版本替换该字符串。

我在每行的html解析和字符串替换方面获得了宝贵的帮助here

要以txt格式打开html文件并逐行浏览,我举了一个示例here

利用两个示例的知识,我编写了以下代码:

from bs4 import BeautifulSoup
from bs4 import SoupStrainer
from html_dictionary import port_eng

def str_replace_port_eng(file_name, tag_name):

with open(file_name, 'rb') as src:
    doc = src.read()
    soup = BeautifulSoup(doc, 'html.parser')
    src.close()

only_tag_name = soup.find_all(str(tag_name))

with open("new_file.html", "w") as outf:
    for line in soup:
        for html_line in range(len(only_tag_name)):
            pt_word = str(only_tag_name[html_line].text).strip()
            pt_word = pt_word.strip('+')
            pt_word = pt_word.strip(' ')

            if pt_word != "":
                en_word = port_eng[pt_word]
                new_line = (str(only_tag_name[html_line]).replace(pt_word, en_word))
                outf.writelines(new_line)
            else:
                en_word = pt_word
                new_line = (str(only_tag_name[html_line]).replace(pt_word, en_word))
                outf.writelines(new_line)

newpg = str_replace_port_eng("input_test.html", "a")

输入文件(示例):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--[if lt IE 7 ]> <html class="ie6" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html xmlns="http://www.w3.org/1999/xhtml"> <!--<![endif]-->

<body>

	<div style="padding-top:0px;height:100%;" id="wrap">
	
		<div style="padding-bottom:0px;" id="header" class="ie-dropdown-fix">
		
		<!-- /// HEADER  //////////////////////////////////////////////////////////////////////////////////////////////////////////// -->

			<div style="margin-left:10px;" class="row">
				<div class="span3">
				
					<!-- // Logo // 
					<a href="index.html" id="logo"><img src="_layout/images/logo.png" alt="" class="responsive-img" /></a>
					-->
					
				</div><!-- end .span3 -->
				<div  style="color:#00233C;width:1100px;background-color:#FFFFFF;margin-right:0px" class="span6">
				
					<!-- // Dropdown Menu // -->
					<ul style="color:#00233C;margin-left:10px;width:1100px;" id="dropdown-menu" class="fixed">
						<li class="current"><a  style="color:#00233C;" href="..."><i class="icon icon-home"></i>  Início</a></li>
						<li><a  style="color:#00233C;margin-left:10px;" href="#"><i class="icon icon-question-sign"></i>  Ajuda <small class="mute">+</small></a>
							<ul class="sub-menu">
								<li><a href="#">FAQ <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Classificação da Informação</a></li>	
										<li><a href="..." target="_blank">Reúso de Ativos Digitais</a></li>												
										<li><a href="..." target="_blank">Biblioteca</a></li>
									</ul>
								</li>							
								<li><a href="#">Alerta <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Criar Alerta</a></li>	
										<li><a href="..." target="_blank">Criar Alerta Múltiplo</a></li>												
									</ul>
								</li>
								<li><a href="..." target="_blank">Aviso ou Notícia</a></li>
								<li><a href="#">Busca <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Busca Simples</a></li>
										<li><a href="..." target="_blank">Busca Avançada</a></li>									
									</ul>
								</li>
								<li><a href="#">Documentos <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Carregar Novo Documento</a></li>
										<li><a href="..." target="_blank">Editar Documento</a></li>													
									</ul>
								</li>
		</div>
	</div>
</body>
</html>

预期输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--[if lt IE 7 ]> <html class="ie6" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if IE 7 ]>    <html class="ie7" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if IE 8 ]>    <html class="ie8" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if IE 9 ]>    <html class="ie9" xmlns="http://www.w3.org/1999/xhtml"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html xmlns="http://www.w3.org/1999/xhtml"> <!--<![endif]-->
    <body>

	<div style="padding-top:0px;height:100%;" id="wrap">
	
		<div style="padding-bottom:0px;" id="header" class="ie-dropdown-fix">
		
		<!-- /// HEADER  //////////////////////////////////////////////////////////////////////////////////////////////////////////// -->

			<div style="margin-left:10px;" class="row">
				<div class="span3">
				
					<!-- // Logo // 
					<a href="index.html" id="logo"><img src="_layout/images/logo.png" alt="" class="responsive-img" /></a>
					-->
					
				</div><!-- end .span3 -->
				<div  style="color:#00233C;width:1100px;background-color:#FFFFFF;margin-right:0px" class="span6">
				
					<!-- // Dropdown Menu // -->
					<ul style="color:#00233C;margin-left:10px;width:1100px;" id="dropdown-menu" class="fixed">
						<li class="current"><a  style="color:#00233C;" href="..."><i class="icon icon-home"></i>  Start</a></li>
						<li><a  style="color:#00233C;margin-left:10px;" href="#"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a>
							<ul class="sub-menu">
								<li><a href="#">FAQ <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Information Security</a></li>	
										<li><a href="..." target="_blank">Digital Asset Reuse</a></li>												
										<li><a href="..." target="_blank">Library</a></li>
									</ul>
								</li>							
								<li><a href="#">Alerta <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Create Alert</a></li>	
										<li><a href="..." target="_blank">Create Multiple Alert</a></li>												
									</ul>
								</li>
								<li><a href="..." target="_blank">News</a></li>
								<li><a href="#">Busca <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Simple Search</a></li>
										<li><a href="..." target="_blank">Advanced Search</a></li>									
									</ul>
								</li>
								<li><a href="#">Documents <small class="mute">+</small></a>
									<ul>
										<li><a href="..." target="_blank">Load New Document</a></li>
										<li><a href="..." target="_blank">Edit Document</a></li>													
									</ul>
								</li>
		</div>
	</div>
</body>
</html>

实际输出:

<a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a><a href="..." style="color:#00233C;"><i class="icon icon-home"></i>  Home</a><a href="#" style="color:#00233C;margin-left:10px;"><i class="icon icon-question-sign"></i>  Help <small class="mute">+</small></a><a href="#">FAQ <small class="mute">+</small></a><a href="..." target="_blank">Information Security</a><a href="..." target="_blank">Reuse of Digital Assets</a><a href="..." target="_blank">Library</a><a href="#">Alert <small class="mute">+</small></a><a href="..." target="_blank">Create Alert</a><a href="..." target="_blank">Create Multiple Alert</a><a href="..." target="_blank">News</a><a href="#">Search <small class="mute">+</small></a><a href="..." target="_blank">Simple Search</a><a href="..." target="_blank">Advanced Search</a><a href="#">Documents <small class="mute">+</small></a><a href="..." target="_blank">Load New Document</a><a href="..." target="_blank">Edit Document</a>

现在我正在寻找代码中的错误,以及如何解决该错误。

预先感谢

Tiago

1 个答案:

答案 0 :(得分:0)

我发现最好的解决方案是复制.html文件中的内容,将其粘贴到.py文件中,然后从那里开始编辑工作。

from bs4 import BeautifulSoup
from html_dictionary import port_eng    # Dictionary
from html_input_file import raw_text    # Input file: .py file with string
                                        # defined by triple quotes (""" """)

rtx = list(raw_text.split('\n'))
ans_list = []                           # List of lines with replaced string
off_list = []                           # List of items not found in dictionary, and index of occurrence

for raw_line in rtx:
    soup = BeautifulSoup(raw_line, "lxml")
    tag_cont = soup.text                # tag content
    tag_cont = tag_cont.strip('+')
    tag_cont = tag_cont.strip(' ')

    if tag_cont in port_eng.keys():
        en_word = port_eng[tag_cont]
        new_item = str(raw_line).replace(tag_cont, en_word)

    else:l
        en_word = tag_cont
        new_item = str(raw_line).replace(tag_cont, en_word)
        if tag_cont not in off_list:    
            off_list.append(tuple([tag_cont, rtx.index(raw_line)]))

    ans_list.append(new_item)

return ans_list, off_list

我通过print()直接从屏幕上获取输出,并将其复制到新的.html文件中-这确实不是最优雅的解决方案,但是可以使用。