当我试图刮掉这个页面时没有输出

时间:2016-10-23 05:03:21

标签: python beautifulsoup

我正在为一个小社区使用一个不和谐机器人,并试图显示特定游戏的在线玩家数量。我在这里使用的代码看起来不错,但这是我第一次抓,所以我可能会要求它寻找错误的关键字。模块加载正常且没有错误,但是当输入触发器以显示信息时,没有任何反应。任何人都可以向我指出小麦我可能错过了或输错了我自己

以下是代码:

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))

1 个答案:

答案 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}}