gpt4 book ai didi

python - Tkinter Reddit scraper(Tkinter 和 Praw)返回错误 "TypeError"

转载 作者:太空宇宙 更新时间:2023-11-03 17:40:55 26 4
gpt4 key购买 nike

我对 python 比较陌生,正在尝试创建一个 tkinter 应用程序,该应用程序从特定的 subreddit 收集信息并在 tkinter 窗口中将其返回给用户。但是,当我运行代码时,它返回错误:

类型错误:“NoneType”对象没有属性“getitem

网络上似乎没有其他方法可以解决此问题,因此我认为在这里提问是合适的。

如果我的代码存在许多其他问题,我深表歉意,请记住,我对编码非常陌生。

谢谢,-杰夫

try:
from Tkinter import *
except ImportError:
from tkinter import *
from webbrowser import open
from datetime import date
import praw

"""
This scraper will (eventually be able to) search a user-defined subreddit and return the top ten posts from that subreddit"
"""

class redditScraper(Frame):

def makeWidgets(self):
intro = "Reddit Client"

Label(self, text="What Subreddit do you wish to view?").pack()
self.e = Entry(self)
self.e.pack(padx=5)

b = Button(self, text="Search Subreddit", command=self.search)
b.pack(pady=5)


def search(self):
user_agent = "Reddit Client (of a sort), by (/u/CowInAFridge)"
r = praw.Reddit(user_agent=user_agent)
posts = r.get_subreddit(self.e.get()).get_hot(limit = 10)
self.makeWidgets.distroy
return posts

def actualFrame(self):

self.newFrame = LabelFrame(self)
self.newFrame.pack(fill="both", expand="yes", anchor = NW)
posts = self.search()
row = 0
for p in posts:
gotoArticle = partial(open, p.url)
title = "(" + str(p.score) + ") " + p.title
Label(self.newFrame, text= title, pady= 10, wraplength= 700, justify= LEFT).grid(row= row, column= 0, sticky= W)
Button(self.newFrame, text= "Read more!", command= gotoArticle).grid(row= row+1, column= 0, sticky= W)
row = row + 2

def __init__(self, master):
Frame.__init__(self, master)
self.makeWidgets()
self.actualFrame()
self.pack()

root = Tk()
app = redditScraper(root)
app.master.title("Reddit Client V.1.0")
app.mainloop()
root.distroy

输出的错误如下:

Traceback (most recent call last):
File "myRedditScraper.py", line 53, in <module>
app = redditScraper(root)
File "myRedditScraper.py", line 49, in __init__
self.actualFrame()
File "myRedditScraper.py", line 37, in actualFrame
posts = self.search()
File "myRedditScraper.py", line 29, in search
posts = r.get_subreddit(self.e.get()).get_hot(limit = 10)
File "/Library/Python/2.7/site-packages/praw/__init__.py", line 1018, in get_subreddit
return objects.Subreddit(self, subreddit_name, *args, **kwargs)
File "/Library/Python/2.7/site-packages/praw/objects.py", line 1356, in __init__
subreddit_name = json_dict['url'].split('/')[2]
TypeError: 'NoneType' object has no attribute '__getitem__'

最佳答案

我认为这里的问题是您在调用 app.mainloop() 之前尝试获取 Entry self.e 中的值。

self.actualFrame() 中,您调用 self.search(),这会进行以下调用:

posts = r.get_subreddit(self.e.get()).get_hot(limit = 10)

在尝试调用 self.e.get() 之前,您必须启动 GUI 的主循环。

我不完全清楚您的代码结构,但如果您尝试从 self.e 检索值,请等待调用 self.search() 直到调用 app.mainloop()

之后

关于python - Tkinter Reddit scraper(Tkinter 和 Praw)返回错误 "TypeError",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30563725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com