Python + BeautifulSoup: How to get wrapper out of HTML based on text?
问题 Would like to get the wrapper of a key text. For example, in HTML: … <div class=“target”>chicken</div> <div class=“not-target”>apple</div> … And by based on the text “chicken”, would like to get back <div class=“target”>chicken</div> . Currently, have the following to fetch the HTML: import requests from bs4 import BeautifulSoup req = requests.get(url).txt soup = BeautifulSoup(r, ‘html.parser’) And having to just do soup.find_all(‘div’,…) and loop through all available div to find the wrapper