Get text with BeautifulSoup CSS Selector
问题 Example HTML <h2 id="name"> ABC <span class="numbers">123</span> <span class="lower">abc</span> </h2> I can get the numbers with something like: soup.select('#name > span.numbers')[0].text How do I get the text ABC using BeautifulSoup and the select function? What about in this case? <div id="name"> <div id="numbers">123</div> ABC </div> 回答1: In the first case, get the previous sibling: soup.select_one('#name > span.numbers').previous_sibling In the second case, get the next sibling: soup