简体   繁体   中英

'exceptions.TypeError' when executing python script

I'm getting the following error when executing the following script:

Error Type: <type 'exceptions.TypeError'>
Error Contents: 'NoneType' object is not iterable
Traceback (most recent call last):
File "addon.py", line 75, in <module>
plugin.run()
File "xbmcswift2/plugin.py", line 332, in run
items = self._dispatch(self.request.path)
File "/plugin.py", line 306, in _dispatch
listitems = view_func(**items)
File "/addon.py", line 42, in all_episodes
items = thisiscriminal.compile_playable_podcast(playable_podcast)
File "/lib/thisiscriminal.py", line 121, in compile_playable_podcast
for podcast in playable_podcast:
TypeError: 'NoneType' object is not iterable

The code in question is as follows, any advice would be greatly appreciated as I have no idea what I'm doing wrong:

def get_playable_podcast(soup):
    """
    @param: parsed html page            
    """
    r    = urllib.urlopen('https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=10000&page=1')
    data = json.loads(r.read().decode('utf-8'))

    for post in data['posts']:

          print post['title']
          print post['episodeNumber']
          print post['audioSource']
          print post['image']['medium']

          subjects = []

          item = {
                'title': post['title'],
                'audioSource': post['audioSource'],
                'episodeNumber': post['episodeNumber'],
                'medium': post['image']['medium']
          }

          subjects.append(item)

          print subjects

def compile_playable_podcast(playable_podcast):
    """
    @para: list containing dict of key/values pairs for playable podcasts
    """
    items = []

    for podcast in playable_podcast:
        items.append({
            post['title']: podcast['title']['episodeNumber'],
            post['audioSource']: podcast['audioSource'],
            post['image']['medium']: podcast['medium'],
            'is_playable': True,})

    return items

I assume your script does something alike to the following,

podcast = get_playable_podcast(soup)
compiled = compile_playable_podcast(podcast)

The problem is that get_playable_podcast has no return statement. In such a case, Python defaults to returning None - which you then pass into compile_playable_podcast . Since None is not iterable, compile_playable_podcast rightfully raises a TypeError .

Now, the solution is of course to return the podcast list you're building in get_playable_podcast , like so,

def get_playable_podcast(soup):
    """
    @param: parsed html page            
    """
    r    = urllib.urlopen('https://thisiscriminal.com/wp-json/criminal/v1/episodes?posts=10000&page=1')
    data = json.loads(r.read().decode('utf-8'))

    subjects = []

    for post in data['posts']:

          print post['title']
          print post['episodeNumber']
          print post['audioSource']
          print post['image']['medium']

          item = {
                'title': post['title'],
                'audioSource': post['audioSource'],
                'episodeNumber': post['episodeNumber'],
                'medium': post['image']['medium']
          }

          subjects.append(item)

          print subjects

    return subjects

Beside this, it may be worthwhile to carefully check your script for unused parameters and/or duplicate code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM