Python BeautifulSoup HTML 解析 class select

Python BeautifulSoup HTML parse class select

@bot.event
async def on_message(message):
    msg = message.content
    if message.content.startswith("https:"):
         response = requests.get(f"https://steamid.io/lookup/" f"{msg}")
         soup = BeautifulSoup(response.text, "html.parser")
         print(soup.prettify())

当我运行代码时,站点的代码写成html

  </div>
  <dl class="panel-body dl-horizontal">
   <dt class="key">
    steamID
   </dt>
   <dd class="value short">
    <img alt="copy to clipboard" class="cp" data-clipboard-text="STEAM_0:0:444916529" data-placement="bottom" data-toggle="tooltip" src="https://steamid.io/static/img/copy.png" title="copy to clipboard"/>
    <a href="https://steamid.io/lookup/STEAM_0:0:444916529" id="a" rel="nofollow">
     STEAM_0:0:444916529
    </a>
   </dd>
   <dt class="key">
    steamID3
   </dt>
   <dd class="value short">
    <img alt="copy to clipboard" class="cp" data-clipboard-text="[U:1:889833058]" data-placement="bottom" data-toggle="tooltip" src="https://steamid.io/static/img/copy.png" title="copy to clipboard"/>
    <a href="https://steamid.io/lookup/[U:1:889833058]" rel="nofollow">
     [U:1:889833058]
    </a>
   </dd>
   <dt class="key">
    steamID64
   </dt>
   <dd class="value short">
    <img alt="copy to clipboard" class="cp" data-clipboard-text="76561198850098786" data-placement="bottom" data-toggle="tooltip" src="https://steamid.io/static/img/copy.png" title="copy to clipboard"/>
    <a href="https://steamid.io/lookup/76561198850098786">
     76561198850098786
    </a>
   </dd>
   <dt class="key">
    customURL
   </dt>
   <dd class="value short">
    not set
   </dd>
   <dt class="key">
    profile state
   </dt>

我想解析并select这些代码中的以下部分

<dt class="key">
    steamID64
   </dt>
   <dd class="value short">
    <img alt="copy to clipboard" class="cp" data-clipboard-text="76561198850098786" data-placement="bottom" data-toggle="tooltip" src="https://steamid.io/static/img/copy.png" title="copy to clipboard"/>
    <a href="https://steamid.io/lookup/76561198850098786">
     76561198850098786
    </a>

我想从这里解析并 select 显示“76561198850098786”的部分,我该怎么做?

您可以使用 :-soup-contains 定位包含术语 steamID64 的 class key 元素,然后使用相邻兄弟组合器移动到相邻元素使用 class value,然后子组合器移动到具有所需值

a 标签
soup.select_one('.key:-soup-contains("steamID64") + .value > a').text