问题
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