Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改HTML文本并保存回HTML_Python_Html_Beautifulsoup - Fatal编程技术网

Python 更改HTML文本并保存回HTML

Python 更改HTML文本并保存回HTML,python,html,beautifulsoup,Python,Html,Beautifulsoup,我正在研究一种简单的方法,将一本用HTML格式的电子书的每一句话包装在span标记中 我正在使用一个经过训练的机器学习模型对句末标点符号(“.!?”)进行分类,并获得真实的句子边界(例如:在美国,“S”不被视为句子) 问题是,为了提供正确的模型数据,我需要首先从我的HTML电子书中提取文本(使用BeautifulSoup的get\u text('\n')) 现在,我能够将get\u text('\n')的输出包装到span标记中。但我不能保存它,因为我丢失了原始HTML电子书中使用的所有其他标记

我正在研究一种简单的方法,将一本用HTML格式的电子书的每一句话包装在span标记中

我正在使用一个经过训练的机器学习模型对句末标点符号(“.!?”)进行分类,并获得真实的句子边界(例如:在美国,“S”不被视为句子)

问题是,为了提供正确的模型数据,我需要首先从我的HTML电子书中提取文本(使用BeautifulSoup的
get\u text('\n')

现在,我能够将
get\u text('\n')
的输出包装到span标记中。但我不能保存它,因为我丢失了原始HTML电子书中使用的所有其他标记

示例HTML电子书示例:

<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><link href="style.css" rel="stylesheet" type="text/css" /><title> Name. Of the book. </title></head> ...
</div>
运行我的算法后:

<span>Name. Of the book.</span>
Name。这本书的作者。
如何获得此输出:

<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><link href="style.css" rel="stylesheet" type="text/css" /><title> <span>Name. Of the book.</span> </title></head> ...
</div>
Name。这本书的作者。。。

提前感谢您的帮助

好吧,我有一个非常天真但非常有效的方法。您可以先获取整个html代码,然后将其存储在字符串中,然后在其上使用
正则表达式
,以提取
span
标记的文本。

这是我现在唯一能想到的方法。希望这有帮助:)

您可以使用
wrap()
方法()将文本包装到
标记中-它将更新整个HTML结构

例如:

data = '''<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><link href="style.css" rel="stylesheet" type="text/css" /><title> Name. Of the book. </title></head>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

print('Before:')
print('-' * 80)
print(soup.prettify())
print('-' * 80)

for text in soup.find_all(text=True):
    text.wrap(soup.new_tag("span"))     # use wrap() function to wrap the text into <span> tag

print('After:')
print('-' * 80)
print(soup.prettify())
print('-' * 80)
data=''名称。这本书的名字
从bs4导入BeautifulSoup
soup=BeautifulSoup(数据'html.parser')
打印('Before:')
打印('-'*80)
打印(soup.prettify())
打印('-'*80)
对于soup.find_all中的文本(text=True):
text.wrap(soup.new_标记(“span”))35;使用wrap()函数将文本包装到标记中
打印('After:')
打印('-'*80)
打印(soup.prettify())
打印('-'*80)
打印(注意
标记内的
):

之前:
--------------------------------------------------------------------------------
名称这本书的作者。
--------------------------------------------------------------------------------
之后:
--------------------------------------------------------------------------------
名称这本书的作者。
--------------------------------------------------------------------------------

创建包含所有所需信息的完整HTML,然后保存。
data = '''<html><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><link href="style.css" rel="stylesheet" type="text/css" /><title> Name. Of the book. </title></head>'''

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'html.parser')

print('Before:')
print('-' * 80)
print(soup.prettify())
print('-' * 80)

for text in soup.find_all(text=True):
    text.wrap(soup.new_tag("span"))     # use wrap() function to wrap the text into <span> tag

print('After:')
print('-' * 80)
print(soup.prettify())
print('-' * 80)
Before:
--------------------------------------------------------------------------------
<html>
 <head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
  <link href="style.css" rel="stylesheet" type="text/css"/>
  <title>
   Name. Of the book.
  </title>
 </head>
</html>
--------------------------------------------------------------------------------
After:
--------------------------------------------------------------------------------
<html>
 <head>
  <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
  <link href="style.css" rel="stylesheet" type="text/css"/>
  <title>
   <span>
    Name. Of the book.
   </span>
  </title>
 </head>
</html>
--------------------------------------------------------------------------------