如何从HTML字符串获取漂亮汤中的开始和结束标签? [英] How to get the opening and closing tag in beautiful soup from HTML string?

查看:101
本文介绍了如何从HTML字符串获取漂亮汤中的开始和结束标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用漂亮的汤来编写python脚本,在这里我必须从包含一些HTML代码的字符串中获取开始标签.

I am writing a python script using beautiful soup, where i have to get an opening tag from a string containing some HTML code.

这是我的字符串:

string = <p>...</p>

我想在名为opening_tag的变量中获取<p>,并在名为closing_tag的变量中获得</p>.我已经搜索了文档,但似乎找不到解决方案.有人可以建议我吗?

I want to get <p> in a variable called opening_tag and </p> in a variable called closing_tag. I have searched the documentation but don't seem to find the solution. Can anyone advise me with that?

推荐答案

没有直接方法可以在BeautifulSoup中获得标签的打开和关闭部分,但是至少可以得到

There is no direct way to get opening and closing parts of the tag in BeautifulSoup, but, at least, you can get the name of it:

>>> from bs4 import BeautifulSoup
>>> 
>>> html_content = """
... <body>
...     <p>test</p>
... </body>
...  """
>>> soup = BeautifulSoup(html_content, "lxml")
>>> p = soup.p
>>> print(p.name)
p

使用 html.parser ,尽管您可以听开始"和结束"标记为事件".

With html.parser though you can listen to "start" and "end" tag "events".

这篇关于如何从HTML字符串获取漂亮汤中的开始和结束标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆