【发布时间】:2020-04-02 02:52:26
【问题描述】:
简而言之,我正在制作一个不和谐的机器人,它将网站https://growtopiagame.com 中的“今日世界”图片下载为 D:\Kelbot/render.png,然后将图片发送到调用该命令的频道.但是,它不是静态网站,而且源代码中也没有URL,所以我找到了使用PyQt5的解决方案:
import re
import bs4 as bs
import sys
import urllib.request
from PyQt5.QtWebEngineWidgets import QWebEnginePage
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
@client.command()
@commands.cooldown(1, 60, commands.BucketType.user)
async def wotd(ctx):
class Page(QWebEnginePage):
def __init__(self, url):
self.app = QApplication(sys.argv)
QWebEnginePage.__init__(self)
self.html = ''
self.loadFinished.connect(self._on_load_finished)
self.load(QUrl(url))
self.app.exec_()
def _on_load_finished(self):
self.html = self.toHtml(self.Callable)
print('Load finished')
def Callable(self, html_str):
self.html = html_str
self.app.quit()
def main():
page = Page('https://growtopiagame.com')
soup = bs.BeautifulSoup(page.html, 'html.parser')
js_test = soup.find('a', class_='world-of-day-image')
link = []
for x in js_test:
link.append(str(x))
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', link[0])
urllib.request.urlretrieve(urls[0], "D:\Kelbot/render.png")
if __name__ == '__main__': main()
await ctx.send(file=discord.File('render.png'))
当我从任务计划程序运行机器人时,它不起作用。因此,我尝试使用我的 Python Shell 和 Visual Studio Code 来运行它,并且它们都有效。但是,当第二次调用该命令时,python shell 和 Visual Studio 代码都重新启动,并且机器人由于某种原因被杀死。是因为类与 discord.py 不兼容吗?我怎么可能解决这个问题。有没有比使用 PyQt5 更好的解决方案?
(有时我没有得到图片,而是得到https://growtopiagame.com/resources/assets/images/load.gif,这是他们在显示实际的每日世界图片之前放置的图像,但当我重新启动我的电脑时它会自行修复)
【问题讨论】:
标签: python web-scraping pyqt pyqt5 discord.py-rewrite