Pycricbuzz Library - Cricket API for Python

As we know, Python has vast libraries and why cricket is untouched. In this tutorial, we will learn about the Python library which is used to get live scores, commentary, and full scorecard for recent and live matches.

To work with the Pycricbuzz, we need to install the Pycricbuzz using the following command.

Press the enter button; it will install pycricbuzz library in the system.

Getting Started with Pycribuzz

Pycricbuzz library is an interesting library as it is loved by the cricket fan. First, create the new python file and import the pycricbuzz.

Now create the Cricbuzz object.

We can use this object to access all the function provided by pycricbuzz. Let's understand the following example.

How to fetch all the matches

Example -

Output:

[
    {
        "id": "21653",
        "mchstate": "toss",
        "mnum": "5th ODI",
        "official": {
            "referee": {
                "country": "Ind",
                "id": "3894",
                "name": "Javagal Srinath"
            },
            "umpire1": {
                "country": "Afg",
                "id": "11150",
                "name": "Bismillah Jan Shinwari"
            },
            "umpire2": {
                "country": "Ind",
                "id": "7498",
                "name": "S Ravi"
            },
            "umpire3": {
                "country": "Afg",
                "id": "11152",
                "name": "Ahmed Shah Pakteen"
            }
        },
        "srs": "Afghanistan v Ireland in India 2019",
        "start_time": "2019-03-10 13:00:00",
        "status": "IRE opt to bowl",
        "team1": {
            "name": "Afghanistan",
            "squad": [
                "Mohammad Shahzad",
                "Javed Ahmadi",
                "Rahmat",
                "Asghar Afghan",
                "Najibullah",
                "Shenwari",
                "Nabi",
                "Rashid Khan",
                "Mujeeb",
                "Zahir Khan",
                "Shapoor"
            ],
            "squad_bench": [
                "Hazratullah Zazai",
                "Noor Ali",
                "Ikram Ali Khil",
                "Naib",
                "Aftab Alam",
                "Dawlat Zadran",
                "Shahidi",
                "Shirzad",
                "Fareed Malik",
                "Karim Janat"
            ]
        },
        "team2": {
            "name": "Ireland",
            "squad": [
                "Porterfield",
                "Stirling",
                "Andy Balbirnie",
                "Simi Singh",
                "Kevin O Brien",
                "Dockrell",
                "Stuart Poynter",
                "Andy McBrine",
                "James Cameron",
                "Murtagh",
                "Rankin"
            ],
            "squad_bench": [
                "Chase",
                "James McCollum",
                "McCarthy",
                "Lorcan Tucker",
                "S Thompson"
            ]
        },
        "toss": "Ireland elect to bowl",
        "type": "ODI",
        "venue_location": "Dehradun, Uttarakhand, India",
        "venue_name": "Rajiv Gandhi International Cricket Stadium"
    }
]

Explanation -

The above code will return the list of the all matches with the relevant details. Each match is associated with the id attribute to fetch the commentary and scorecard for that match. We can also get the match details using the below method.

How to fetch live score

We create the live_score() function which takes the match id. Let's understand the following code.

Example -

Output:

{
    "batting": {
        "batsman": [
            {
                "balls": "43",
                "fours": "0",
                "name": "Asghar Afghan",
                "runs": "8",
                "six": "0"
            },
            {
                "balls": "32",
                "fours": "1",
                "name": "Nabi",
                "runs": "19",
                "six": "0"
            }
        ],
        "score": [
            {
                "declare": null,
                "inning_num": "1",
                "overs": "25.2",
                "runs": "77",
                "wickets": "4"
            }
        ],
        "team": "Afghanistan"
    },
    "bowling": {
        "bowler": [
            {
                "maidens": "0",
                "name": "James Cameron",
                "overs": "4.2",
                "runs": "15",
                "wickets": "0"
            }
        ],
        "score": [],
        "team": "Ireland"
    }
}

Explanation -

When we run the above code, it returns the match and details of the batting and bowling team. The batsman and bowlers are the current two batsman batting and current two bowlers bowling.

How to fetch commentary of the match

In the below method, we create the function which takes match id. Using the commentary() method, we get the names of the commentary. Let's understand the following example.

Example -

Output:

{
    "commentary": [
        {
            "comm": "Simi Singh to Nabi, no run",
            "over": "26.2"
        },
        {
            "comm": "Simi Singh to Nabi, no run, tossed up outside off, Nabi gets an inside edge on the clip to short fine",
            "over": "26.1"
        },
        {
            "comm": "Asghar Afghan is still struggling from the shoulder injury he sustained in the last match. Winced in pain after that last six, but going on for his team",
            "over": null
        },
        {
            "comm": "James Cameron to Asghar Afghan, no run, tossed up on off, Afghan leans well forward and blocks",
            "over": "25.6"
        },
        {
            "comm": "James Cameron to Asghar Afghan, no run, flighted on the leg-stump line, clipped straight to the fielder at backward leg, wanted a single and is sent abck",
            "over": "25.5"
        },
        {
            "comm": "James Cameron to Asghar Afghan, <b>SIX</b>, that has been <b>thumped</b>, juicy full-toss from Cameron-Dow, Afghan moves across and slugs it high and over backward square leg for a maximum",
            "over": "25.4"
        },
        {
            "comm": "James Cameron to Asghar Afghan, <b>FOUR</b>, that's a poor ball from Cameron-Dow, dropped short and wide of off, Afghan made room and slapped it wide of cover - beats the fielder getting across from the deep",
            "over": "25.3"
        },
        {
            "comm": "James Cameron to Asghar Afghan, no run, dropped short and wide of off, cracked straight to Dockrell at cover",
            "over": "25.2"
        },
        {
            "comm": "James Cameron to Asghar Afghan, no run, turn for Cameron-Dow, but once again the length is short, allows Afghan to push it to the off-side",
            "over": "25.1"
        },
        {
            "comm": "Simi Singh to Nabi, no run, full and at the stumps, whipped straight to the fielder at mid-wicket",
            "over": "24.6"
        }
    ]
}

Explanation -

"Commentary" is a list containing all the commentary texts.

How to fetch scorecard of a match

Let's understand the following example.

Example -

Output:

{
    "scorecard": [
        {
            "batcard": [
                {
                    "balls": "4",
                    "dismissal": "c James Cameron b Murtagh",
                    "fours": "0",
                    "name": "Mohammad Shahzad",
                    "runs": "6",
                    "six": "1"
                },
                {
                    "balls": "30",
                    "dismissal": " b Andy McBrine",
                    "fours": "2",
                    "name": "Javed Ahmadi",
                    "runs": "24",
                    "six": "1"
                },
                {
                    "balls": "42",
                    "dismissal": "c Stirling b Dockrell",
                    "fours": "0",
                    "name": "Rahmat",
                    "runs": "17",
                    "six": "1"
                },
                {
                    "balls": "47",
                    "dismissal": "batting",
                    "fours": "1",
                    "name": "Asghar Afghan",
                    "runs": "18",
                    "six": "1"
                },
                {
                    "balls": "1",
                    "dismissal": "c Andy McBrine b Dockrell",
                    "fours": "0",
                    "name": "Shenwari",
                    "runs": "0",
                    "six": "0"
                },
                {
                    "balls": "38",
                    "dismissal": "batting",
                    "fours": "1",
                    "name": "Nabi",
                    "runs": "20",
                    "six": "0"
                }
            ],
            "batteam": "Afghanistan",
            "bowlcard": [
                {
                    "maidens": "1",
                    "name": "Murtagh",
                    "nballs": "0",
                    "overs": "5",
                    "runs": "18",
                    "wickets": "1",
                    "wides": "0"
                },
                {
                    "maidens": "0",
                    "name": "Rankin",
                    "nballs": "0",
                    "overs": "3",
                    "runs": "13",
                    "wickets": "0",
                    "wides": "1"
                },
                {
                    "maidens": "1",
                    "name": "Andy McBrine",
                    "nballs": "0",
                    "overs": "5",
                    "runs": "7",
                    "wickets": "1",
                    "wides": "1"
                },
                {
                    "maidens": "0",
                    "name": "Dockrell",
                    "nballs": "0",
                    "overs": "6",
                    "runs": "18",
                    "wickets": "2",
                    "wides": "0"
                },
                {
                    "maidens": "0",
                    "name": "James Cameron",
                    "nballs": "0",
                    "overs": "5",
                    "runs": "25",
                    "wickets": "0",
                    "wides": "1"
                },
                {
                    "maidens": "0",
                    "name": "Simi Singh",
                    "nballs": "0",
                    "overs": "3",
                    "runs": "7",
                    "wickets": "0",
                    "wides": "0"
                }
            ],
            "bowlteam": "Ireland",
            "extras": {
                "byes": "0",
                "lbyes": "0",
                "nballs": "0",
                "penalty": "0",
                "total": "3",
                "wides": "3"
            },
            "fall_wickets": [
                {
                    "name": "Mohammad Shahzad",
                    "overs": "0.4",
                    "score": "6",
                    "wkt_num": "1"
                },
                {
                    "name": "Javed Ahmadi",
                    "overs": "11.3",
                    "score": "39",
                    "wkt_num": "2"
                },
                {
                    "name": "Rahmat",
                    "overs": "14.3",
                    "score": "50",
                    "wkt_num": "3"
                },
                {
                    "name": "Shenwari",
                    "overs": "14.4",
                    "score": "50",
                    "wkt_num": "4"
                }
            ],
            "inng_num": "1",
            "overs": "27",
            "runs": "88",
            "wickets": "4"
        }
    ]
}





Latest Courses