网页抓取时禁止走动

时间:2018-08-08 06:50:50

标签: python http

我正在尝试使discord机器人在调用命令时打印单个整数。这是我目前的尝试,我知道我缺少一些重要的东西。

import discord
from discord.ext import commands
from discord.ext.commands import bot
from bs4 import BeautifulSoup
import requests
import urllib.request
from urllib.request import Request, urlopen
site = "site"

def getInfo():
    hdr = {'User-agent' : 'Mozilla/5.0'}
    req = Request(site,headers=hdr)
    page = urlopen(req)
    response = requests.get(site)
    soup = BeautifulSoup('span', 'html.parser')



getInfo()

bot = commands.Bot(command_prefix ='!')

@bot.event
async def on_ready():
    print ("Prefix is !")
@bot.command(pass_context=True)
async def pcount(ctx):
    await bot.say(getInfo())


bot.run("Key")

我很确定我的许多进口商品都是多余的,但这似乎不是问题。我对python很陌生,这是我的第二个真正的项目。

当前问题是,由于以下原因,什么都不会引起矛盾。我叫!pcount命令,这是错误。

Ignoring exception in command pcount
Traceback (most recent call last):
  File "C:\Users\Zane\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "bot.py", line 28, in pcount
    await bot.say(getInfo())
  File "C:\Users\Zane\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 309, in _augmented_msg
    msg = yield from coro
  File "C:\Users\Zane\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 1152, in send_message
    data = yield from self.http.send_message(channel_id, content, guild_id=guild_id, tts=tts, embed=embed)
  File "C:\Users\Zane\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\http.py", line 200, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: BAD REQUEST (status code: 400): Cannot send an empty message

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Zane\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\Zane\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\Zane\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: HTTPException: BAD REQUEST (status code: 400): Cannot send an empty message

我知道错误是因为Discord有一个过滤器来阻止僵尸程序发送不包含任何内容的消息,但是我不确定如何将响应对象读取到例如想要的内容中,对于该项目,我希望从中获取一个变量网站。

1 个答案:

答案 0 :(得分:0)

我只是通过使用请求并使用BS解析HTML数据来解决了这个问题。这就是我所拥有的。

import discord
from discord.ext import commands
from discord.ext.commands import bot
from bs4 import BeautifulSoup as soup
import requests
import urllib.request
from urllib.request import Request, urlopen
site = "SITE URL"

def open():
    #headers = {'User-Agent' : 'Mozilla/5.0'}

    rec = requests.get(site)
    soupPage = soup(rec.text, "html.parser")
    players = soupPage.find("span", {"id" : "HTML_num_players"})
    #print(players)
    playersString = str(players)
    playersEdit = playersString.strip('<span id="HTML_num_players">/')
    #print (playersEdit)
    return playersEdit






bot = commands.Bot(command_prefix ='!')

@bot.event
async def on_ready():
    print ("Prefix is !, jackass")
@bot.command(pass_context=True)
async def playercount(ctx):
    await bot.say("Current playercount is " + open())
@bot.command(pass_context=True)
async def pc(ctx):
    await bot.say("Current playercount is " + open())




bot.run("KEY")