2016-10-23 5 views
0

小さなコミュニティのためにdiscordボットを使用し、特定のゲームにオンラインプレーヤーの数を表示しようとしています。私にここで使っているコードは大丈夫だと思われますが、これは私が初めて掻き集めることです。間違ったキーワードを探すよう求めているかもしれません。モジュールはエラーなしで正常にロードされますが、トリガを入力して情報を表示すると何も起こりません。誰も私に私が見逃しているかもしれ小麦や間違った入力自分自身ここでこのページから削り取ろうとしたときに出力がありません

を指摘することができ、コードです:

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

画像をポストする代わりにテキストにテキストをコピーして貼り付けてください。 – jwodder

+0

完了しましたが、それはいくつかのそれを壊しました。 –

答えて

0

あなたの最初の問題は、それがid=ないid_=、末尾のアンダースコアでなければなりませんです。

soupObject.find(id='online_section') 

次の問題は、それがどのように見えるです:それはのJsを使用してレンダリングされているので

<div id="online_section"> 
     Loading currently playing player counts... 

</div> 

。幸いなことに、データを簡単に取得できる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}} 
関連する問題