让它工作的最好的方法是什么?由于Selenium与asyncio不能很好地协同工作,机器人最终将会死亡。它按预期工作,但最终会崩溃。我确实理解这个问题是什么,特别是考虑到selenium是如此沉重,但是有没有好的解决办法?
我得到了错误消息:任务已被销毁,但它正在等待!
我没有其他的东西,没有回溯。我必须重启机器人(不一致),它会再次工作一段时间。
@bot.command(pass_context=True)
async def weather(ctx, arg):
if ctx:
list = ('lf','oahu')
await bot.say('*Loading webdriver.....*')
if arg in list:
if arg == 'lf':
website = '--'
name = 'Los Feliz Map'
if arg == 'oahu':
website = '--'
name = 'Oahu Map'
load_site = webdriver.Chrome()
# load site on chrome
load_site.get(website)
await bot.say('*Loading ' + str(name) + '...*')
load_site.find_element_by_xpath('//*[@id="main-map"]/div[2]/div[1]/div/a[2]').click()
load_site.find_element_by_xpath('//*[@id="main-map"]/div[2]/div[2]').click()
load_site.find_element_by_xpath('//*[@id="main-map"]/div[2]/div[2]/div/form/div[3]/label[6]/div/span').click()
await asyncio.sleep(2) #sleep to ensure weather loads on site before screenshot
load_site.save_screenshot('weather.png')
await bot.say('*Taking screenshot and saving image....*')
await bot.send_file(ctx.message.channel, 'weather.png')
load_site.quit()
print('Succesfully sent image of ' + str(arg) + ' - ' + str(ctx.message.author.name))
我省略了这个网站,因为它是一个私人网站。
发布于 2021-06-24 17:26:45
简短的回答:这是不可能的。如果你设法让它工作,它就不会工作得很好。
冗长的答案: Selenium是一个阻塞API,您不能将其与异步脚本一起使用。如果你真的想使用网络浏览器,你可以试着找一个像https://pypi.org/project/aioselenium/这样的替代品。
发布于 2021-08-21 08:09:44
您可以在使用discord.py的同时,使用aiohttp向API发出请求。
https://stackoverflow.com/questions/48895857
复制相似问题