以下是代码:
import discord
from discord.ext import commands
try: # check if BeautifulSoup4 is installed
from bs4 import BeautifulSoup
soupAvailable = True
except:
soupAvailable = False
import aiohttp
class bf1online:
"""My custom cog that does stuff!"""
def __init__(self, bot):
self.bot = bot
"""This does stuff!"""
#Your code will go here
@commands.command()
async def bf1(self):
"""How many players are online atm?"""
#Your code will go here
url = "http://bf1stats.com/" #build the web adress
async with aiohttp.get(url) as response:
soupObject = BeautifulSoup(await response.text(), "html.parser")
try:
online = soupObject.find(id_='online_section').find('h2').find('p').find('b').get_text()
await self.bot.say(online + ' players are playing this game at the moment')
except:
await self.bot.say("Couldn't load amount of players. No one is playing this game anymore or there's an error.")
def setup(bot):
bot.add_cog(bf1online(bot))
答案 0 :(得分:0)
您的第一个问题是它应该是id=
而不是id_=
,没有尾随下划线。
soupObject.find(id='online_section')
下一个问题是:
<div id="online_section">
Loading currently playing player counts...
</div>
因为那是使用Js渲染的。幸运的是,你可以模仿很容易获得数据的ajax调用:
In [1]: import requests
...: data =
requests.get("http://api.bf1stats.com/api/onlinePlayers").json()
...:
In [2]: data
Out[2]:
{'pc': {'count': 126870, 'label': 'PC', 'peak24': 179935},
'ps4': {'count': 237504, 'label': 'PS4', 'peak24': 358182},
'xone': {'count': 98474, 'label': 'XBOXONE', 'peak24': 266869}}