我在Python中创建了一个Discord Bot,我想全天候在线,所以我将它上传到我的服务器。
Bot正在我的Windows PC上完全正常工作,我创建了这个Bot。 我已经制作了一个html包装器来获取论坛类别的最新论坛链接。
如果我在Windows上启动Bot它可以工作。我会得到链接等。
但如果在我的Ubuntu服务器上启动它,我将收到以下错误:
Ignoring exception in command patchnotes
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/discord/ext/commands/core.py", line 50, in wrapped
ret = yield from coro(*args, **kwargs)
File "launcher.py", line 67, in patchnotes
newspage.raise_for_status()
File "/usr/lib/python3/dist-packages/requests/models.py", line 840, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: http://board.asharu.com/index.php?board/2-news-game
我用apt-cache search -n ^python3
检查了已安装的python3模块,并尝试通过Python Shell导入我需要的所有模块而不会出现错误,所以我想我需要安装所有模块。
问题是,我无法解释,为什么我只在我的Ubuntu服务器上出现此错误。
以下是我使用的代码:
# command Patchnotes
@client.command(pass_context=True)
async def patchnotes(ctx, *arg):
"""
Get's the newest Topic of the Category "News Game" from board.asharu.com
and send the link to the newest Topic to the chat.
"""
#try:
newspage = requests.get('http://board.asharu.com/index.php?board/2-news-game')
newspage.raise_for_status()
#except requests.exceptions.RequestException as err:
#return await client.say(err)
tree = html.fromstring(newspage.content)
for link_element in tree.xpath('//li[contains(@class, "columnSubject")]//h3//a'):
href = link_element.get('href')
return await client.say(href)
我使用的导入是:
import discord
import logging
from lxml import html
import requests
from discord.ext import commands
我还尝试设置UserAgent但没有成功:
newspage = requests.get('http://board.asharu.com/index.php?board/2-news-game', headers={'User-Agent': 'Mozilla/5.0'})
newspage.raise_for_status()
我尝试删除newspage.raise_for_status()
并打印解码后的内容并获得:
</span>
<span class="cf-footer-separator">•</span>
<span class="cf-footer-item"><span data-translate="performance_security_by">Performance & security by</span> <a data-orig-proto="https" data-orig-ref="www.cloudflare.com/5xx-error-landing?utm_source=error_footer" id="brand_link" target="_blank">Cloudflare</a></span>
</p>
</div><!-- /.error-footer -->
</div><!-- /#cf-error-details -->
</div><!-- /#cf-wrapper -->
<script type="text/javascript">
window._cf_translation = {};
</script>
</body>
</html>
所以我猜Ubuntu服务器被阻止了Cloudflare甚至将用户代理作为标题。
在我的Windows上,我会得到一个更大的html代码,我想要它。