不和谐和机器人制作:我似乎无法让我的机器人发布网络解析材料

时间:2018-05-06 09:08:33

标签: python beautifulsoup html-parsing bots discord

第一篇文章!是的......所以。我制作机器人并遇到了麻烦。

我似乎无法让我的机器人在调用" stats"时在我的不和谐频道中发布网络解析的资料。我写的命令。我得到了#39; 403未经授权的#34;在调用命令时!我试图显示一些统计数据。

使用python:

import asyncio
import datetime
from difflib import SequenceMatcher
import json
import urllib
from bs4 import BeautifulSoup
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import requests
import html2text
from html.parser import HTMLParser
from html.entities import name2codepoint
client = discord.Client()

bot = commands.Bot(command_prefix='!', description='a halo server status check bot.')
#responce = requests.get(url)
#responce.text[:100]
import urllib.request

url = "http://chiperdilly.com/cestatstext/test.html"
html = urllib.request.urlopen(url).read()
mystr = html.decode("utf8")

soup = BeautifulSoup(html, "html.parser")
stringdata = (element.text for element in soup.find_all("div", "value"))
chanel = client.get_channel("437430094445674500")
for script in soup(["script", "style"]):
    script.extract

@bot.event
async def on_ready():
    print('dont let dreams be dreams...')
    print(bot.user.name)
    print(bot.user.id)

@bot.event
async def on_message(message):
    if message.content.startswith('!stats'):
        #await client.send_message(message.channel, content=stringdata)
        bot.say(message.channel, stringdata)
        #print(stringdata)
bot.run(myTOKEN)

使用的HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Halo CE Server Stats tool</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta http-equiv="refresh" content="10">
        <style type="text/css">
        </style>

        <link href="css/rhp_customized.css" rel="stylesheet" type="text/css">
    </head>
    <body background="images/hp_bg.jpg">
        <table width="699" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td><img src="images/hp_top.jpg" width="699" height="58" /></td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg"><img src="images/hp_serverinfo.jpg" width="699" height="44" /></td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg" style="padding-left: 35px; padding-right: 50px;">
      <table width="600" border="0" align="center" cellpadding="3" cellspacing="1">
        <tr>
          <td valign="top">
            <div align="center" class="value"><strong><font size="+1">!tht_serber!</font></strong><br />
            <span class="smalltext" class="value">Dedicated  server v01.00.10.0621</span><br /><br />
            </div>
            <table width="100%">
              <tr>
                <td>
                  Address: <strong><a href="halogame.com:2302">halogame.com:2302</a></strong><br />
                  This is a <strong>public</strong> server<br />
                  There are <b>1</b> / <b>16</b> players<br />
                  Current game is <b>CTF</b> (CTF)<br />
                  The Frag/Score Limit is <b>3</b>
                </td>
                <td valign="top">
                  <div align="center" class="value"><strong>Team Scores:</strong><br />
                  <span class="red"><b>Red Team: 0</b></span><br /><span class="blue"><b>Blue Team: 0</b></span></div>
                </td>
              </tr>
            </table>
            </div>
          </td>
          <td>
            <div align="center" class="value"><a href="http://hce.halomaps.org/index.cfm?search=Bloodgulch"><img src="images/maps/bloodgulch.jpg" width="200" height="150" id="map_illustration" alt="Halo Bigtits map"></a><br />
            <strong>Bloodgulch</strong></div><br />
          </td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg"><img src="images/hp_playerinfo.jpg" width="699" height="47" /></td>
  </tr>
  <tr>
    <td background="images/hp_tablebg.jpg" style="padding-left: 35px; padding-right: 50px;"><table width="100%" border="0" cellpadding="3">
    <tr>
        <td width="33%" class="bigbold" align="center">
            Player Name
        </td>
        <td width="33%" class="bigbold" align="center">
            Score
        </td>
        <td width="34%" class="bigbold" align="center">
            Team
        </td>
    </tr>
    <tr>
        <td width="33%" align="center">
            somedude
        </td>
        <td width="33%" align="center">
            0
        </td>
        <td width="34%" align="center">
            <span class="red"><strong>RED</strong></span>
        </td>
    </tr>
</table></td>
  </tr>
  <tr>
    <td><img src="images/hp_bottom.jpg" width="699" height="49" /></td>
  </tr>
</table>
        <br />

    </body>
</html>

有谁知道我如何能够通过http 403问题?并获取我的代码将表值粘贴到聊天中?

编辑:现在已经过了403而不是401:

追踪(最近一次通话):   文件&#34; C:\ Python36_64 \ lib \ site-packages \ discord \ client.py&#34;,第307行,在_run_event中     来自getattr的收益率(自我,事件)(* args,** kwargs)   在on_message中输入文件&#34; halobot.py&#34;,第43行     await client.send_message(message.channel,content = stringdata)   send_message中的文件&#34; C:\ Python36_64 \ lib \ site-packages \ discord \ client.py&#34;,第1152行     data = yield来自self.http.send_message(channel_id,content,guild_id = guild_id,tts = tts,embed = embed)   文件&#34; C:\ Python36_64 \ lib \ site-packages \ discord \ http.py&#34;,第200行,请求中     提出HTTPException(r,data) discord.errors.HTTPException:UNAUTHORIZED(状态代码:401):401:未经授权

1 个答案:

答案 0 :(得分:0)

您的请求缺少请求标头。您必须指定Web服务器允许您连接的User-Agent标头。