gpt4 book ai didi

Python Facebook 'NoneType object error'

转载 作者:行者123 更新时间:2023-12-01 05:28:26 24 4
gpt4 key购买 nike

我是编程和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' object is not iterable”。

最佳答案

FacebookSearchsearch 方法末尾添加返回查询。您实际上要求它解压当前返回的 searchNone 。当函数没有显式定义的return时,它默认返回None

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

关于Python Facebook 'NoneType object error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20862043/

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