How to get the opening and closing tag in beautiful soup from HTML string?

北城余情 提交于 2019-12-25 09:17:10

问题


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

Here is my string:

string = <p>...</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?


回答1:


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

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



来源:https://stackoverflow.com/questions/41597854/how-to-get-the-opening-and-closing-tag-in-beautiful-soup-from-html-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!