最近在做一些 Instagram 相关的项目,Python 上并没有可靠的 Instagram API,所以决定直接使用 requests 库。以下为代码段:
def getMe(): while True: mediaMe = requests.get('https://api.instagram.com/v1/users/self/media/recent/?access_token={0}&count=1'.format(access_token)) print mediaMe.content print type(mediaMe.content) yield ast.literal_eval(mediaMe.content) with open('instagram.log', 'a') as log: getMe() for line in getMe(): log.write(json.dumps(line)) log.write('\n') print type(line) 然后问题出现了,mediaMe 返回的是一个类,mediaMe.content 返回的类型是一个字符串,这个字符串是被字符化的一个词典。尝试使用 ast.literal_eval(mediaMe.content) 转换为词典的时候,却提示 Malformed string。请问有什么解决的方法吗?先提前谢谢大家。
