Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我正在制作一个discord机器人,它发送新上传的书籍,我收到了这个错误_Python_Python 3.x_Discord_Discord.py_Bots - Fatal编程技术网

Python 我正在制作一个discord机器人,它发送新上传的书籍,我收到了这个错误

Python 我正在制作一个discord机器人,它发送新上传的书籍,我收到了这个错误,python,python-3.x,discord,discord.py,bots,Python,Python 3.x,Discord,Discord.py,Bots,当我使用时,问题就出现了!第一次命令我得到的错误是 Ignoring exception in command firsttime: Traceback (most recent call last): File "goodreads.py", line 27, in firsttime_command for link in links.reverse(): TypeError: 'NoneType' object is not iterable 上述异常是

当我使用
时,问题就出现了!第一次
命令我得到的错误是

Ignoring exception in command firsttime:

Traceback (most recent call last):
  File "goodreads.py", line 27, in firsttime_command
    for link in links.reverse():
TypeError: 'NoneType' object is not iterable
上述异常是以下异常的直接原因

这是密码

import re
import json
import aiohttp
from datetime import datetime

import discord
from discord.ext import commands, tasks

JSON_PATH = "json file path"
REGEX = "<a class=readable bookTitle href=(.*)[?].*>"
URL = "https://www.goodreads.com/genres/new_releases/fantasy"
CHANNEL_ID = 834867425677803580

class Goodreads(commands.Cog):
  def __init__(self, bot):
    self.bot = bot

  @commands.Cog.listener()
  async def on_ready(self):
    self.check_website.start()

  @commands.command(name="firsttime")
  async def firsttime_command(self, ctx):
    links = await self.make_request()
    data = {}
    now = str(datetime.utcnow())
    for link in links.reverse():
      data[link] = now
    with open(JSON_PATH, "w") as f:
      json.dump(data, f, indent=2)

  @tasks.loop(minutes=1)
  async def check_website(self):
    links = await self.make_request()

with open(JSON_PATH, "r") as f:
  data = json.load(f)

for link in links:
  if link not in data.keys():
    await self.bot.get_channel(CHANNEL_ID).send(f"A new fantasy book released.\n{link}")
    data[link] = str(datetime.utcnow())
    with open(JSON_PATH, "w") as f:
      json.dump(data, f, indent=2)

  async def make_request(self):
    async with aiohttp.ClientSession() as ses:
      async with ses.get(URL) as res:
        text = await res.text()
        text = text.replace("\\\"", "")
        return re.findall(REGEX, text)

bot = commands.Bot(command_prefix="!")
bot.add_cog(Goodreads(bot))

@bot.event
async def on_connect():
  print("Connected")

@bot.event
async def on_ready():
  print("Ready")

bot.run("tokens")
重新导入
导入json
进口aiohttp
从日期时间导入日期时间
进口不和
从discord.ext导入命令、任务
JSON_PATH=“JSON文件路径”
REGEX=“”
URL=”https://www.goodreads.com/genres/new_releases/fantasy"
通道ID=834867425677803580
类Goodreads(commands.Cog):
def uuu init uuuu(自我,机器人):
self.bot=bot
@commands.Cog.listener()
异步def on_就绪(自):
self.check_网站.start()
@commands.command(name=“firsttime”)
异步def firsttime_命令(self,ctx):
links=等待self.make_请求()
数据={}
now=str(datetime.utcnow())
对于links.reverse()中的链接:
数据[链接]=现在
将open(JSON_路径,“w”)作为f:
dump(数据,f,缩进=2)
@tasks.loop(分钟=1)
异步def检查_网站(自):
links=等待self.make_请求()
将open(JSON_路径,“r”)作为f:
data=json.load(f)
对于链接中的链接:
如果链接不在data.keys()中:
wait self.bot.get_channel(channel_ID).send(f“发布了一本新的幻想书。\n{link}”)
data[link]=str(datetime.utcnow())
将open(JSON_路径,“w”)作为f:
dump(数据,f,缩进=2)
异步def生成请求(自):
与aiohttp.ClientSession()作为ses异步:
与ses.get(URL)作为res异步:
text=wait res.text()
text=text。替换(“\\\”,“”)
返回关于findall(正则表达式,文本)
bot=commands.bot(命令前缀=“!”)
bot.添加_cog(Goodreads(bot))
@机器人事件
_connect()上的异步定义:
打印(“连接”)
@机器人事件
_ready()上的异步定义:
打印(“就绪”)
bot.run(“代币”)

在深入挖掘代码并使用
print()
检查变量中的值后,我发现所有问题都已解决

.reverse() 
它在适当的位置工作——因此它改变了原始列表中的顺序,并返回None

你必须这样做

 links.reverse()

 for link in links:
     #... code ...
或者您应该使用
reversed()

或者您可以使用
slice
进行此操作

 for link in links[::-1]:
     #... code ...


顺便说一句:
list.sort()和
sorted(list)相同

上面写着:
上面的异常是下面异常的直接原因
,下面的异常是什么?我想我在另一个问题中看到了这段代码。你重复了吗?似乎你仍然没有学会如何调试代码-即使使用
print()
。如果代码显示您在哪一行出错,那么首先您可以使用
print()
检查变量中的值-您似乎在
links
中得到
None
,然后运行
links.reverse()
,这意味着
None.reverse()
。当您获得
None
时,您应该跳过所有代码。如果您从`self.make\u request()`获得
链接
,那么您应该签入
make\u request()
变量中的内容-可能您使用了错误的值或在某些页面上使用了
findall(REGEX,…)
找不到元素,它给出了
None
。同样,您可以使用
pritn()
要调试它-检查您从服务器获得的HTML。可能它发送的HTML与您期望的不同-即,它可能会发送错误消息、机器人警告或重新跟踪等。所以很快您必须调试代码-并检查变量中的所有内容-不要信任代码。您以前测试过您的正则表达式吗?这是错误的。我在第页和第页检查了HTML不是
class=readable bookTitle
,而是
class=\“readable bookTitle\”
href=\“\“
而不是
href=…
。顺便说一句:你必须记住,
aiohttp
可以获取HTML,但它不能运行
JavaScript
——所以如果你在浏览器中手动检查HTML,那么首先关闭
JavaScript
 for link in links[::-1]:
     #... code ...