Turgut Cecen
Turgut Cecen

Reputation: 27

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())

When I run the code, the codes of the site are written as 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>

I want to parse and select the following part from those codes

<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>

I want to parse and select the section that says "76561198850098786" from here, how can I do it?

Upvotes: 0

Views: 128

Answers (1)

QHarr
QHarr

Reputation: 84465

You can use the :-soup-contains to target the element with class key containing the term steamID64, then use an adjacent sibling combinator to move to the adjacent element with class value, then a child combinator to move to the a tag having the desired value

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

Upvotes: 2

Related Questions