我是python的新手,在解释事物时,请使其对初学者友好:)。 基本上,当我尝试使用tkinter运行reddit爬虫时,会遇到各种我不理解的错误,在此先感谢您,对社区感到非常感谢。
以下是一些基本代码:
def Exas():
import praw
imgcount = 0
reddit = praw.Reddit(client_id='CENSORED',
client_secret='CENSORED',
user_agent='TheBigScraper(By u/scrappermaster)',
username='scrappermaster',
password='thescrapperisscrap')
listoftitles = []
listofurls = []
# whichone = input('what subreddit do you want to access? ')
# endlimit = input('what number of pictures do you want to download? ')
whichoner = whichone
ender = int(endlimit.get())
subreddit = reddit.subreddit(whichone)
for submission in subreddit.hot(limit=int(ender)):
title = submission.title
link = submission.url
Both = title + " " + link
if '.jpg' in link :
listofurls.append(link)
listoftitles.append(title)
大约50行以下:
import tkinter as tk
import colorama
root = tk.Tk()
root.title("InstagramBot")
root.geometry('320x125')
whichone = str(tk.StringVar())
endlimit = tk.StringVar()
lblWhichone = tk.Label(root, text = 'Subreddit Name:').grid(row = 0, column = 0, padx = 0, pady = 10)
entWhichone = tk.Entry(root, textvariable = whichone).grid(row = 0, column = 1)
lblIntendlimit = tk.Label(root, text = 'Number of Pictures:').grid(row = 1, column = 0, padx = 0, pady = 10)
entendlimit = tk.Entry(root, textvariable = endlimit).grid(row = 1, column = 1)
btn = tk.Button(root, text = 'Execute', command = Exas, fg='red', font='Helvetica 18 bold').grid(row = 5, column = 1)
root.mainloop()
root.title("InstagramBot")
奇怪的错误代码:
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/Users/eucar/Documents/Testing/Compactor2000.py", line 28, in Exas
for submission in subreddit.hot(limit=int(ender)):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/models/listing/generator.py", line 52, in __next__
self._next_batch()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/models/listing/generator.py", line 62, in _next_batch
self._listing = self._reddit.get(self.url, params=self.params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/reddit.py", line 408, in get
data = self.request('GET', path, params=params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/praw/reddit.py", line 534, in request
params=params)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prawcore/sessions.py", line 185, in request
params=params, url=url)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/prawcore/sessions.py", line 130, in _request_with_retries
raise self.STATUS_EXCEPTIONS[response.status_code](response)
prawcore.exceptions.Redirect: Redirect to /subreddits/search
答案 0 :(得分:0)
我已将Exas降低到基于Tkinter的最低要求。我从来没有使用过praw库,也没有安装它。
def Exas():
whichoner = whichone.get()
ender = int(endlimit.get())
print(whichoner, ender)
为我在评论中提到的StingVar修改tkinter窗口。
import tkinter as tk
root = tk.Tk()
root.title("InstagramBot")
root.geometry('320x125+50+50')
whichone = tk.StringVar()
endlimit = tk.StringVar()
lblWhichone = tk.Label(root, text = 'Subreddit Name:').grid(row = 0, column = 0, padx = 0, pady = 10)
entWhichone = tk.Entry(root, textvariable = whichone).grid(row = 0, column = 1)
# The grid method returns None! All these variables have the value None.
# I don't think it matters for what you're doing but may cause problems for you in the future.
# In general this is better.
# entWhichone = tk.Entry(root, textvariable = whichone)
# entWhichone.grid(row = 0, column = 1)
# entWhichone now points to a tk.Entry object.
lblIntendlimit = tk.Label(root, text = 'Number of Pictures:').grid(row = 1, column = 0, padx = 0, pady = 10)
entendlimit = tk.Entry(root, textvariable = endlimit).grid(row = 1, column = 1)
btn = tk.Button(root, text = 'Execute', command = Exas, fg='red', font='Helvetica 18 bold').grid(row = 5, column = 1)
root.mainloop()
如果我在您的窗口中键入test和42,它会将Test和42回显到我的控制台。 您最新的错误消息表明StringVar已传递给praw函数或方法。在Exas功能中,您需要:
whichoner = whichone.get() # Was it left at ... = whichone ?
如果上述建议仍无济于事,请尝试在调用其他任何东西之前先打印whichoner和ender,以检查它们是否返回您的期望值。
一个风格要点是,在Python中,函数和变量通常使用小写字母,并带有下划线“ _”,而不是lblWhichone,而将lbl_which_one拆分为Exas。类以大写tk命名。Entry创建一个Entry类的对象。您的代码仍然可以工作,其他人难以理解。