卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章64334本站已运行4115

Python程序提取HTML标签之间的字符串

Python程序提取HTML标签之间的字符串

HTML标签用于设计网站的框架。我们通过在标签中包含的字符串形式传递信息和上传内容。HTML标签之间的字符串决定了元素在浏览器中的显示和解释方式。因此,提取这些字符串在数据操作和处理中起着至关重要的作用。我们可以分析和理解HTML文档的结构。

这些字符串揭示了构建网页背后的隐藏模式和逻辑。在本文中,我们将处理这些字符串。我们的任务是提取HTML标签之间的字符串。

理解问题

我们需要提取在HTML标记之间的所有字符串。我们的目标字符串被不同类型的标记包围,只有内容部分应该被检索出来。让我们通过一个例子来理解这个问题。

输入输出场景

让我们考虑一个字符串 -

Input:
Inp_STR = "<h1>This is a test string,</h1><p>Let's code together</p>"

输入字符串由不同的HTML标签组成,我们需要提取它们之间的字符串。

Output: [" This is a test string,  Let's code together "]

正如我们所看到的,"<h1>"和"<p>"标签被移除了,字符串被提取出来。现在我们已经理解了问题,让我们讨论一下几个解决方案。

使用迭代和替换()

这种方法专注于消除和替换HTML标签。我们将传递一个字符串和一个不同HTML标签的列表。之后,我们将将此字符串初始化为列表的一个元素。

我们将遍历标签列表中的每个元素,并检查它是否存在于原始字符串中。我们将传递一个“pos”变量,它将存储索引值并驱动迭代过程。

我们将使用“replace()”方法将每个标签替换为一个空格,并获取一个没有HTML标签的字符串。

Example

的中文翻译为:

示例

以下是一个示例,用于提取HTML标签之间的字符串 -

Inp_STR = "<h1>This is a test string,</h1><p>Let's code together</p>"
tags = ["<h1>", "</h1>", "<p>", "</p>", "<b>", "</b>", "<br>"]
print(f"This is the original string: {Inp_STR}")
ExStr = [Inp_STR]
pos = 0

for tag in tags:
   if tag in ExStr[pos]:
      ExStr[pos] = ExStr[pos].replace(tag, " ")
pos += 1

print(f"The extracted string is : {ExStr}")

输出

This is the original string: <h1>This is a test string,</h1><p>Let's code together</p>
The extracted string is : [" This is a test string,  Let's code together "]

使用正则表达式模块 + findall()

在这种方法中,我们将使用正则表达式模块来匹配特定的模式。我们将传递一个正则表达式:“<"+tag+">(.*?)</"+tag+">”,该表达式表示目标模式。该模式旨在捕获开放和关闭标签。在这里,“tag”是一个变量,通过迭代从标签列表中获取其值。

findall()”函数用于在原始字符串中找到模式的所有匹配项。我们将使用“extend()”方法将所有的“匹配项”添加到一个新的列表中。通过这种方式,我们将提取出HTML标签中包含的字符串。

Example

的中文翻译为:

示例

以下是一个示例 -

import re
Inp_STR = "<h1>This is a test string,</h1><p>Let's code together</p>"
tags = ["h1", "p", "b", "br"]
print(f"This is the original string: {Inp_STR}")
ExStr = []

for tag in tags:
   seq = "<"+tag+">(.*?)</"+tag+">"
   matches = re.findall(seq, Inp_STR)
   ExStr.extend(matches)
print(f"The extracted string is: {ExStr}")

输出

This is the original string: <h1>This is a test string,</h1><p>Let's code together</p>
The extracted string is: ['This is a test string,', "Let's code together"]

使用迭代和find()函数

在这种方法中,我们将使用“find()”方法获取原始字符串中开放和关闭标签的第一个出现。我们将遍历标签列表中的每个元素,并检索其在字符串中的位置。

将使用While循环来继续在字符串中搜索HTML标签。我们将建立一个条件来检查字符串中是否存在不完整的标签。在每次迭代中,索引值将被更新以找到下一个开放和关闭标签的出现。

所有开放和关闭标签的索引值都被存储,一旦整个字符串被映射,我们使用字符串切片来提取HTML标签之间的字符串。

Example

的中文翻译为:

示例

以下是一个示例 -

Inp_STR = "<h1>This is a test string,</h1><p>Let's code together</p>"
tags = ["h1", "p", "b", "br"]
ExStr = []
print(f"The original string is: {Inp_STR}")

for tag in tags:
   tagpos1 = Inp_STR.find("<"+tag+">")
   while tagpos1 != -1:
      tagpos2 = Inp_STR.find("</"+tag+">", tagpos1)
      if tagpos2 == -1:
         break
      ExStr.append(Inp_STR[tagpos1 + len(tag)+2: tagpos2])
      tagpos1 = Inp_STR.find("<"+tag+">", tagpos2)

print(f"The extracted string is: {ExStr}")

输出

The original string is: <h1>This is a test string,</h1><p>Let's code together</p>
The extracted string is: ['This is a test string,', "Let's code together"]

结论

在本文中,我们讨论了许多提取HTML标签之间字符串的方法。我们从更简单的解决方案开始,定位并替换标签为空格。我们还使用了正则表达式模块及其findall()函数来找到匹配的模式。我们还了解了find()方法和字符串切片的应用。

卓越飞翔博客
上一篇: 如何在PHP和Vue.js中实现可交互的热力图统计
下一篇: PHP学习心得:如何调试代码
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏