Skip to content
Advertisement

How to identify a link from href based on string and class name?

I’m trying to get some data from https://betsapi.com/, specifically from the soccer area using python I saw in the code that the link is dynamic, I mean that a couple of weeks ago it was https://betsapi.com/cin/soccer and now is https://betsapi.com/cip/soccer.

Looking on the code I would like to understand how to identify the current soccer link from this part of code.

<div class="card-tabs text-center">
            <a href="/" class="card-tabs-item active">
        All (70)
      </a>
                <a href="/cip/basketball" class="card-tabs-item"></a>
                <a href="/cip/soccer" class="card-tabs-item"></a>
                <a href="/cip/horse-racing" class="card-tabs-item"> </a>
                <a href="/cip/greyhounds" class="card-tabs-item"></a>
                <a href="/cip/ice-hockey" class="card-tabs-item"></a>
                <a href="/cip/table-tennis" class="card-tabs-item"></a>
                <a href="/cip/volleyball" class="card-tabs-item"></a>                                                    
      <div class="dropdown show">
      <a href="#" class="card-tabs-item" data-toggle="dropdown" aria-expanded="true">More</a>
      <div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow show" x-placement="bottom-end" style="position: absolute; transform: translate3d(-109px, 55px, 0px); top: 0px; left: 0px; will-change: transform;">
                                    <a class="dropdown-item " href="/cip/golf"></a>
                                    <a class="dropdown-item " href="/cip/tennis"></a>
                                    <a class="dropdown-item " href="/cip/baseball"></a>
                                    <a class="dropdown-item " href="/cip/esports"></a>
                                    <a class="dropdown-item " href="/cip/darts"></a>
                                    <a class="dropdown-item " href="/cip/handball"></a>
                                    <a class="dropdown-item " href="/cip/futsal"></a>รน

Many thanks

Advertisement

Answer

I would just search through the card tab items and look for 'soccer'. Then print the href to get the link:

import requests
from bs4 import BeautifulSoup

url = 'https://betsapi.com'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.141 Safari/537.36'}
response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')

cards = soup.find_all('a', {'class':'card-tabs-item'})
soccer = [x for x in cards if 'soccer' in x['href']][0]
link = url + soccer['href']

Output:

print(link)
https://betsapi.com/cip/soccer
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement