How to fetch anime viewing data from MAL profiles using a Discord bot?

I’m working on a Discord bot and I want it to grab anime stats from MyAnimeList profiles. You know, stuff like how many shows a user has finished, is currently watching, or dropped. I’ve got the profile URL format figured out, but I’m stuck on actually getting the data.

Here’s what I’m trying to do:

  1. Take a username
  2. Go to their MAL profile
  3. Scrape their anime stats

I’ve looked around but can’t find a clear way to do this. Any ideas on how to make it work? Maybe there’s an API or a library I should use?

async def get_mal_stats(username):
    # Need help here!
    profile_url = f'https://myanimelist.net/profile/{username}'
    # How do I extract the stats from the profile page?
    return anime_stats

# Usage in Discord bot
@bot.command()
async def anime_stats(ctx, mal_username):
    stats = await get_mal_stats(mal_username)
    await ctx.send(f'{mal_username} stats: {stats}')

Has anyone done something like this before? What’s the best approach?

Hey there! I actually ran into a similar issue when building my anime-themed Discord bot. Instead of web scraping, which can be finicky and break easily, I ended up using the official MyAnimeList API. It’s a bit more work to set up initially, but way more stable in the long run.

First, you’ll need to register your application on MAL to get API credentials. Then you can use the mal-api Python library to interact with it. Here’s a quick example of how I structured my code:

from mal import Anime, AnimeSearch

async def get_mal_stats(username):
    search = AnimeSearch(username)
    user = search.results[0]
    stats = user.get_statistics()
    return stats

# Rest of your Discord bot code here

This approach gives you direct access to all the user’s anime stats without worrying about HTML parsing or site changes. Just remember to handle API rate limits and potential errors gracefully in your bot’s logic. Good luck with your project!

yo, i’ve done smthing similar b4. check out beautifulsoup4 library for python. it’s great for scraping web pages. here’s a quick example:

import requests
from bs4 import BeautifulSoup

async def get_mal_stats(username):
    url = f'https://myanimelist.net/profile/{username}'
    page = requests.get(url)
    soup = BeautifulSoup(page.content, 'html.parser')
    # extract stats here
    return stats

this should get u started. gl with ur project!

I’ve actually tackled a similar project before. For fetching MAL data, I’d recommend using the Jikan API. It’s an unofficial API for MyAnimeList that’s well-documented and easier to work with than web scraping.

You’ll need to install the Jikan wrapper for Python. Then, you can modify your code like this:

from jikan import Jikan
jikan = Jikan()

async def get_mal_stats(username):
    user_data = jikan.user(username=username, request='profile')
    anime_stats = user_data['anime_stats']
    return anime_stats

# Usage remains the same

This approach is more reliable and less likely to break if MAL changes their HTML structure. Plus, it’s faster and puts less strain on MAL’s servers. Just be mindful of API rate limits when implementing this in your Discord bot.