beautifulsoup官方文档

时间:2025-04-26 08:23:50
html_doc = """ <html><head><title>The Dormouse's story</title></head> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="/elsie" class="sister" >Elsie</a>, <a href="/lacie" class="sister" >Lacie</a> and <a href="/tillie" class="sister" >Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> """ print(BeautifulSoup(html_doc, "", parse_only=only_a_tags).prettify()) # <a class="sister" href="/elsie" > # Elsie # </a> # <a class="sister" href="/lacie" > # Lacie # </a> # <a class="sister" href="/tillie" > # Tillie # </a> print(BeautifulSoup(html_doc, "", parse_only=only_tags_with_id_link2).prettify()) # <a class="sister" href="/lacie" > # Lacie # </a> print(BeautifulSoup(html_doc, "", parse_only=only_short_strings).prettify()) # Elsie # , # Lacie # and # Tillie # ... #