Python Facebook'NoneType对象错误'

时间:2013-12-31 18:55:56

标签: python facebook python-2.7

我是编程和python的新手。我在下面写了这个脚本,我得到一个“TypeError:'NoneType'对象不可迭代”

import csv
import json
import urllib
import sys
import time
import re

class FacebookSearch:
    def __init__(self,
        query   = '"https://graph.facebook.com/search.{mode}?{query}&{access_token}'
    ):
        access_token = 'XXXXX|XXXXX'

    def search(self, q, mode='json', **queryargs):
        queryargs['q'] = q
        query = urllib.urlencode(queryargs)


def write_csv(fname, rows, header=None, append=False, **kwargs):
    filemode = 'ab' if append else 'wb'
    with open(fname, filemode) as outf:
        out_csv = csv.writer(outf, **kwargs)
        if header:
            out_csv.writerow(header)
        out_csv.writerows(rows)

def main():
    ts = FacebookSearch()
    response, data = ts.search('appliance', result_type='recent')  #I am getting the error on this line
    js = json.loads(data)



    messages = ([msg['created_time'],  msg['id']] for msg in js.get('data', []))

    write_csv('fb_washerdryer.csv', messages, append=True)

if __name__ == '__main__':
    main()

我不确定为什么我在我定义图API的搜索词的行上得到“TypeError:'NoneType'对象不可迭代”。

1 个答案:

答案 0 :(得分:2)

return query FacebookSearch方法的末尾添加search。您实际上是要求它解包None当前正在返回的search。当函数没有明确定义return时,它默认返回None

>>> response, data = None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable