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.
JavaScript
x
22
22
1
<div class="card-tabs text-center">
2
<a href="/" class="card-tabs-item active">
3
All (70)
4
</a>
5
<a href="/cip/basketball" class="card-tabs-item"></a>
6
<a href="/cip/soccer" class="card-tabs-item"></a>
7
<a href="/cip/horse-racing" class="card-tabs-item"> </a>
8
<a href="/cip/greyhounds" class="card-tabs-item"></a>
9
<a href="/cip/ice-hockey" class="card-tabs-item"></a>
10
<a href="/cip/table-tennis" class="card-tabs-item"></a>
11
<a href="/cip/volleyball" class="card-tabs-item"></a>
12
<div class="dropdown show">
13
<a href="#" class="card-tabs-item" data-toggle="dropdown" aria-expanded="true">More</a>
14
<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;">
15
<a class="dropdown-item " href="/cip/golf"></a>
16
<a class="dropdown-item " href="/cip/tennis"></a>
17
<a class="dropdown-item " href="/cip/baseball"></a>
18
<a class="dropdown-item " href="/cip/esports"></a>
19
<a class="dropdown-item " href="/cip/darts"></a>
20
<a class="dropdown-item " href="/cip/handball"></a>
21
<a class="dropdown-item " href="/cip/futsal"></a>ù
22
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:
JavaScript
1
12
12
1
import requests
2
from bs4 import BeautifulSoup
3
4
url = 'https://betsapi.com'
5
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'}
6
response = requests.get(url, headers=headers)
7
soup = BeautifulSoup(response.text, 'html.parser')
8
9
cards = soup.find_all('a', {'class':'card-tabs-item'})
10
soccer = [x for x in cards if 'soccer' in x['href']][0]
11
link = url + soccer['href']
12
Output:
JavaScript
1
3
1
print(link)
2
https://betsapi.com/cip/soccer
3