我试图只从h2和第一个p标签中获取文本。我一直在使用类名来查找div,输出给我div中的所有文本(显然)。
以下是HTML:
<div class="horoscope-content">
<h2> Today's Libra Horoscope for January 27, 2022 <span class="today-badge">TODAY</span></h2>
<p>Go with the flow, Libra. If you find that a situation isn't unfolding the way you'd like it to, take it as a sign to back off. Swimming upstream is hard work, so use your energy more efficiently by exploring different options. When you step back from a stressful situation, circumstances could turn around. Lighten up by considering other possibilities or talking it through with a helpful friend.</p>
<p>What's in the stars for you tomorrow? <a href="/horoscopes/daily/libra/friday">Read it now</a>.</p>
<div class="dropdown-inline">Read the <b>daily horoscope</b> for another zodiac sign:<div id="dropdown_below_horoscope_dropdown" class="dropdown">
以下是我使用的代码:
libra_content = driver.find_elements(By.CLASS_NAME, 'horoscope-content')
我假设答案是使用xpath,但我不知道如何同时包含这两个标记。我需要使用两行独立的代码来完成这项工作,还是可以将这两行代码合并为一行?