Site
Home / Forum / Developers / Get all soccer player name only

Get all soccer player name only


Posted: 27 Sep 2023 12:51
Carbon
Posts: 8
Joined: 2023-09-27

Hello,

I want to build a custom json with like :

[
{
"name": "Sergio Ramos",
},
]

I want to have all possible name (only football soccer player) in sports db database.
I already have an answer, and zag told me to make id by id.
But when i do a loop from id 0 to the end of the db, all player have no data ?
What can i do to have only soccer player name ?


Posted: 27 Sep 2023 12:55

zag
Posts: 3,330
Joined: 2020-03-23

You would need to create a loop with the player ID and move up one id each time.

https://www.thesportsdb.com/api/v1/json/3/lookupplayer.php?id=34145937
then:
https://www.thesportsdb.com/api/v1/json/3/lookupplayer.php?id=34145938
etc

Posted: 27 Sep 2023 13:00
Carbon
Posts: 8
Joined: 2023-09-27

Ok but how do i know where the soccer id player begin ? do we know the start id of the loop ?


Posted: 27 Sep 2023 14:48

zag
Posts: 3,330
Joined: 2020-03-23

Ok but how do i know where the soccer id player begin ? do we know the start id of the loop ?


English Premier league player contains the lowest ID players I believe.

EDIT: Federico Macheda is first (34145337)

Posted: 27 Sep 2023 14:54
Carbon
Posts: 8
Joined: 2023-09-27

Ok so i start the index at 34145337 and do we know how many soccer player only are present in db ?
Because if i dont end the loop i will get other sport player and i need only soccer ?

Posted: 27 Sep 2023 19:06

zag
Posts: 3,330
Joined: 2020-03-23

Ok so i start the index at 34145337 and do we know how many soccer player only are present in db ?
Because if i dont end the loop i will get other sport player and i need only soccer ?


Well in your code, you would stop when you find a null generally.

But also you can just check the front page for the stats

Posted: 28 Sep 2023 13:28
Carbon
Posts: 8
Joined: 2023-09-27

Hello Zag,

I made a little python script to scrap all the footballer name in the api.

import requests

# URL de l'API de TheSportsDB
base_url = "https://www.thesportsdb.com/api/v1/json/3/lookupplayer.php?id="
index = 34145337 # Premier index

while True:
url = f"{base_url}{index}"

# Effectuer la demande GET à l'API
response = requests.get(url)

# Vérifier si la réponse est vide (null)
data = response.json()
if not data.get('players'):
print(f"Null response for id {index}. Stopping here.")
break

# Récupérer le champ 'strPlayer' de chaque joueur dans la réponse
players = data['players']
for player in players:
strPlayer = player.get('strPlayer')
if strPlayer:
print(f"Player Name : {strPlayer}")

# Incrémentez l'index pour la prochaine demande
index += 1

Even if i know the starting index, i cannot stop when response is null because some of the players have no data in the middle of the others.

python3 scrap.py
Player Name : Federico Macheda
Player Name : Danny Gabbidon
Player Name : Kagisho Dikgacoi
Player Name : Anthony Pilkington
Player Name : David Marshall
Player Name : Joe Lewis
Null response for id 34145343. Stopping here.

Do you know the last index so i can made a loop ith know start index and know end index.

Thank you

Posted: 19 Oct 2023 13:04
Carbon
Posts: 8
Joined: 2023-09-27

Up !

Does anyone has an idea ?

Posted: 19 Oct 2023 13:28

zag
Posts: 3,330
Joined: 2020-03-23

Yes last is 34222354

But you will find many missing ID's as we delete some players over time. You just need to handle those NULL's

Posted: 19 Oct 2023 13:46
Carbon
Posts: 8
Joined: 2023-09-27

Thank you

I just continue the loop if player is null.
nervermind for the null player.

I maybe find another solution :

if data.get('players'):
# Récupérer le champ 'strPlayer' de chaque joueur dans la réponse
players = data['players']
for player in players:
if (player.get('strSport') == "Soccer" and player.get('strGender') == "Male"):
strPlayer = player.get('strPlayer')
if strPlayer:
data_list.append({"name": strPlayer})

I check if strSport of player is Soccer, if true i get the player name.

But problem of this solution is, i will continue to scrap the api eventho soccer players are finish



Who is Online?

In total there are 68 users online :: 3 registered, 0 hidden and 65 guests (based on users active over the past 5 minutes) Most users ever online was 424 on Fri Nov 10, 2017 9:02 pm

About Us

Discussion forum for TheSportsDB.com site and related topics

Rules

- Be Polite
- Respect other users
- Always post log files with issues
- Try to be helpful
- No Piracy discussion

Showing 0 to 10 (Total: 10)