Turgut Cecen
Turgut Cecen

Reputation: 27

Python BeautifulSoup HTML parse code select

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

When I run the code, the codes of the site are written as html

<table class="table-striped table-hover">
        <tr>
         <td style="width:40%">
          a steamID
         </td>
         <td>
          <code>
           STEAM_0:0:11101
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a steamID3
         </td>
         <td>
          <code>
           [U:1:22202]
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a steamID3 without brackets
         </td>
         <td>
          <code>
           U:1:22202
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a steamID64
         </td>
         <td>
          <code>
           76561197960287930
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a customURL
         </td>
         <td>
          <code>
           gabelogannewell
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a full URL
         </td>
         <td>
          <code>
           http://steamcommunity.com/profiles/76561197960287930
          </code>
         </td>
        </tr>
        <tr>
         <td>
          a full URL with customURL
         </td>
         <td>
          <code>
           http://steamcommunity.com/id/gabelogannewell
          </code>
         </td>
        </tr>
       </table>
       <p>
        <br/>
        Regular names as input may work and will yield a limited list of profiles, but only if the matching customURL is not taken. customURLs always prevail.
       </p>
       <p>
        Trade URLs will
        <i>
         NOT
        </i>
        work.
       </p>
       <p>
        To convert or look up multiple values, use the dropdown arrow to select
        <i>
         extended list
        </i>
        .
       </p>
      </div>
     </section>
    </div>
   </div>

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

    </tr>
        <tr>
         <td>
          a steamID64
         </td>
         <td>
          <code>
           76561197960287930
          </code>
         </td>
        </tr>

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

Upvotes: 1

Views: 52

Answers (1)

MendelG
MendelG

Reputation: 20088

You find a <tr> that contains the text "a steamID64" and find the <code> tag:

soup = BeautifulSoup(html, "html.parser")

print(
    soup.select_one("tr:-soup-contains('a steamID64') code").get_text(strip=True)
)

Output:

76561197960287930z

Upvotes: 1

Related Questions