TypeError:'NoneType' 对象不可迭代,Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20009316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeError: 'NoneType' object is not iterable, Python
提问by DeadZero
I have a made a few posts of the last few days , so thanks for the help so far, all of it is appreciated for helping out a beginner.
我最近几天发了一些帖子,所以感谢到目前为止的帮助,感谢所有帮助初学者的帖子。
Heads Up , I know the code below looks erm very basic and there is probably a lost simpler and more elegant way to achieve what I am attempting, but for now this is the basics I can learn and actually understand to a degree haha.
注意,我知道下面的代码看起来非常基本,并且可能有一种丢失的更简单和更优雅的方法来实现我正在尝试的东西,但现在这是我可以学习并在一定程度上真正理解的基础知识哈哈。
Just wondering if someone can actually tell me what the error is caused by,on the program output, it seems to run ok sometimes and fail with this error maybe 2 out of 3.
只是想知道是否有人真的可以告诉我错误是由什么引起的,在程序输出上,它有时似乎运行正常,但由于这个错误可能有 2 次失败。
this is still a work in progress project , the main purpose is creating a program that I can automate something I do manually as a learning environment.
这仍然是一个正在进行的项目,主要目的是创建一个程序,作为学习环境,我可以自动执行一些我手动执行的操作。
Thanks for any Insight.
感谢您的任何见解。
Program Output
程序输出
Beginning Mining
Opener Created
Opener Headers Added
Url Assigned
HTTP Error 503: Service Temporarily Unavailable
1
Opener Created
Opener Headers Added
Url Assigned
Page Open and Read
90378
soup assigned to rawdata
filtered for torType Class
reassigned to soup to tordata
Links Filtered
Printing Recent Torrents from Filter WebData function
['/the-big-bang-theory-s07e08-hdtv-x264-lol-ettv-t8180359.html', '/the-walking-dead-s04e05-hdtv-x264-2hd-ettv-t8158163.html', '/the-vampire-diaries-s05e07-proper-hdtv-x264-2hd-ettv-t8180537.html', '/the-big-bang-theory-7x7-hdtv-lol-t8141134.html', '/how-i-met-your-mother-s09e09-hdtv-x264-2hd-eztv-t8164029.html', '/sons-of-anarchy-s06e10-hdtv-x264-asap-ettv-t8170162.html', '/american-horror-story-s03e06-hdtv-x264-killers-ettv-t8175485.html', '/sleepy-hollow-s01e07-hdtv-x264-lol-eztv-t8164318.html', '/elementary-2x8-hdtv-lol-t8180367.html', '/the-originals-s01e07-hdtv-x264-lol-ettv-t8169688.html', '/the-blacklist-s01e07-hdtv-x264-lol-eztv-t8124923.html', '/modern-family-s05e07-hdtv-x264-2hd-eztv-t8175344.html', '/arrow-s02e06-proper-hdtv-x264-2hd-eztv-t8179975.html', '/white-collar-s05e05-hdtv-x264-2hd-eztv-t8180828.html', '/homeland-s03e07-720p-hdtv-x264-killers-eztv-t8158378.html', '/greys-anatomy-s10e09-hdtv-x264-lol-ettv-t8180608.html', '/how-i-met-your-mother-s09e08-hdtv-x264-2hd-eztv-t8124354.html', '/arrow-s02e05-hdtv-x264-lol-ettv-t8135416.html', '/boardwalk-empire-s04e10-hdtv-x264-2hd-eztv-t8158693.html', '/the-simpsons-s25e04-hdtv-x264-lol-eztv-t8157997.html', '/modern-family-s05e06-hdtv-x264-asap-eztv-t8061643.html', '/new-girl-s03e07-hdtv-x264-lol-eztv-t8130237.html', '/the-tomorrow-people-us-s01e06-hdtv-x264-lol-eztv-t8174964.html', '/the-walking-dead-s04e04-hdtv-x264-2hd-ettv-t8118639.html', '/supernatural-s09e06-hdtv-x264-lol-ettv-t8169899.html']
Printing newdata which should be Recent Torrents from Filter WebData function
['/the-big-bang-theory-s07e08-hdtv-x264-lol-ettv-t8180359.html', '/the-walking-dead-s04e05-hdtv-x264-2hd-ettv-t8158163.html', '/the-vampire-diaries-s05e07-proper-hdtv-x264-2hd-ettv-t8180537.html', '/the-big-bang-theory-7x7-hdtv-lol-t8141134.html', '/how-i-met-your-mother-s09e09-hdtv-x264-2hd-eztv-t8164029.html', '/sons-of-anarchy-s06e10-hdtv-x264-asap-ettv-t8170162.html', '/american-horror-story-s03e06-hdtv-x264-killers-ettv-t8175485.html', '/sleepy-hollow-s01e07-hdtv-x264-lol-eztv-t8164318.html', '/elementary-2x8-hdtv-lol-t8180367.html', '/the-originals-s01e07-hdtv-x264-lol-ettv-t8169688.html', '/the-blacklist-s01e07-hdtv-x264-lol-eztv-t8124923.html', '/modern-family-s05e07-hdtv-x264-2hd-eztv-t8175344.html', '/arrow-s02e06-proper-hdtv-x264-2hd-eztv-t8179975.html', '/white-collar-s05e05-hdtv-x264-2hd-eztv-t8180828.html', '/homeland-s03e07-720p-hdtv-x264-killers-eztv-t8158378.html', '/greys-anatomy-s10e09-hdtv-x264-lol-ettv-t8180608.html', '/how-i-met-your-mother-s09e08-hdtv-x264-2hd-eztv-t8124354.html', '/arrow-s02e05-hdtv-x264-lol-ettv-t8135416.html', '/boardwalk-empire-s04e10-hdtv-x264-2hd-eztv-t8158693.html', '/the-simpsons-s25e04-hdtv-x264-lol-eztv-t8157997.html', '/modern-family-s05e06-hdtv-x264-asap-eztv-t8061643.html', '/new-girl-s03e07-hdtv-x264-lol-eztv-t8130237.html', '/the-tomorrow-people-us-s01e06-hdtv-x264-lol-eztv-t8174964.html', '/the-walking-dead-s04e04-hdtv-x264-2hd-ettv-t8118639.html', '/supernatural-s09e06-hdtv-x264-lol-ettv-t8169899.html']
Mining Ended
Relative Search
Diagnostic line ['*arrow*', '*grimm*', '*elementary*', '*American*Horror*', '*once*Upon*aTime*', '*Agents*of*shield*']
Diagnostic line None
Searching for My Torrents
Reset Vars
Diagnostic line *arrow*
Traceback (most recent call last):
File "C:\Python33\scratchpad.py", line 121, in <module>
SearchTorrents(MyPrograms,RecentTorrentLinks)
File "C:\Python33\scratchpad.py", line 103, in SearchTorrents
matchobj = fnmatch.filter(TorrentList,item)
File "C:\Python33\lib\fnmatch.py", line 59, in filter
for name in names:
TypeError: 'NoneType' object is not iterable
Program Code
程序代码
WebUrl = 'http://kickasstorrents.come.in/tv/'
MyPrograms = ['*arrow*','*grimm*','*elementary*','*American*Horror*','*once*Upon*aTime*','*Agents*of*shield*']
loop = 0
RecentTorrentLinks = []
def RetrieveWebData(MyUrl):
try:
#create Url Opener
opener = urllib.request.build_opener()
#Diagnostic Line
print( "Opener Created")
#Simulation as a browser
opener.addheaders =[('User-agent','Mozilla/5.0')]
#Diagnostic Line
print( "Opener Headers Added")
url = (MyUrl)
#Diagnostic Line
print( "Url Assigned")
#Open URL and read Data
page = opener.open(url).read()
#Diagnostic Line
print( "Page Open and Read")
#Diagnostic Line
print(len(page))
newdata = FilterWebData(page)
# Diagnotic Line
print ("Printing newdata which should be Recent Torrents from Filter WebData function")
print (newdata)
return newdata
except urllib.error.URLError as e:
print(e)
if e.code == 503:
print (loop ++ 1)
RetrieveWebData(WebUrl)
else:
print('Error Occured :-' + e + ' Code :- '+ e.code)
sys.exit()
def FilterWebData(RawData):
RecentTors = []
try:
soup = BeautifulSoup(RawData)
#Diagnostic Line
print (" soup assigned to rawdata")
TorData = soup.find_all("a", {"class" : "torType"})
#Diagnostic Line
print (" filtered for torType Class")
soup = BeautifulSoup(str(TorData))
#Diagnostic Line
print (" reassigned to soup to tordata")
for link in soup.find_all('a'):
RecentTors.append(link.get('href'))
#Diagnostic Line
print ("Links Filtered")
# Diagnotic Line
print ("Printing Recent Torrents from Filter WebData function")
print (RecentTors)
return RecentTors
#Simpler Version of my code by StackOverlfow User
#TorData = soup.find_all("a", {"class" : "torType", 'href': True})
#return [link['href'] for link in TorData]
except OSError as e:
print("An error occurred during filtering",e)
def SearchTorrents(SearchCrit,TorrentList):
try:
#Diagnostic Line
print ('Diagnostic line ' + str(SearchCrit))
print ('Diagnostic line ' + str(TorrentList))
#Diagnostic Line
print ("Searching for My Torrents")
item = " "
matchobj = " "
#Diagnostic Line
print ("Reset Vars")
for item in SearchCrit:
#Diagnostic Line
print ('Diagnostic line ' + item)
#print ('Diagnostic line ' + str(TorrentList))
matchobj = fnmatch.filter(TorrentList,item)
print (matchobj)
except OSError as e:
print("An error occurred during searching",e)
#Diagnostic Line
print ("Beginning Mining")
RecentTorrentLinks = RetrieveWebData(WebUrl)
#Diagnostic Line
print ("Mining Ended")
#Diagnostic Line
print ("Relative Search")
#Diagnostic Line
#print ("Diagnotic Line TorrentLinks ")
#print (RecentTorrentLinks)
SearchTorrents(MyPrograms,RecentTorrentLinks)
#Diagnostic Line
print ("Relative Ended")
采纳答案by codegeek
Your TorrentListis actually Nonewhich means you cannot iterate over it.
您TorrentList实际上是None,这意味着您无法对其进行迭代。
Change this line
改变这一行
matchobj = fnmatch.filter(TorrentList,item)
to
到
if TorrentList is not None:
matchobj = fnmatch.filter(TorrentList,item)
else:
# Throw whatever error/exception you need
回答by forgivenson
It looks like the variable TorrentList is sometimes not a list, but instead is NoneType. So, when you attempt to filter it, it throws the error. You should add a check in your code to make sure it is actually a list before filtering it.
看起来变量 TorrentList 有时不是列表,而是 NoneType。因此,当您尝试对其进行过滤时,它会引发错误。您应该在代码中添加一个检查以确保它在过滤之前实际上是一个列表。
You can see in your output, before the error, these lines are printed:
您可以在输出中看到,在出现错误之前,打印了以下几行:
Diagnostic line ['*arrow*', '*grimm*', '*elementary*', '*American*Horror*', '*once*Upon*aTime*', '*Agents*of*shield*']
Diagnostic line None
Those are the output of the following lines:
这些是以下几行的输出:
print ('Diagnostic line ' + str(SearchCrit))
print ('Diagnostic line ' + str(TorrentList))
As you can see, TorrentList prints out 'None', instead of a list. So, as I said above, when you try to filter this on the following line:
如您所见,TorrentList 打印出“无”,而不是一个列表。因此,正如我上面所说,当您尝试在以下行中进行过滤时:
matchobj = fnmatch.filter(TorrentList,item)
it throws the error.
它抛出错误。

