・response結果をmongoDBに書き込みたいのですが、書き込めません。
どなたかご教授願いたいです。
操作内容
① ツイッターデータを読み込む
②google cloud platform の Natural language API を用いて文章の感情分析を行う
③その分析結果をmongoDBに書き込む
①と②は実行できましたが、③が実行できません。
よろしくお願いします。
Python
1# -*- coding: utf-8 -*- 2#!/usr/bin/env python 3# -*- coding: utf-8 -*- 4import got3 as got 5import sys 6import requests 7 8from pymongo import MongoClient 9 10client = MongoClient('localhost', 27017) 11 12db = client.sample2 13 14#こっから感情分析 15tweetCriteria = got.manager.TweetCriteria().setQuerySearch('検索したいワード').setSince("2018-6-1").setUntil( 16 "2018-6-30").setMaxTweets(2) 17tweet = got.manager.TweetManager.getTweets(tweetCriteria) 18 19for v in tweet: 20 def main(content, access_token): 21 url = 'https://language.googleapis.com/v1/documents:analyzeSentiment?key={}'.format(access_token) 22 header = {'Content-Type': 'application/json'} 23 body = { 24 "document": { 25 "type": "PLAIN_TEXT", 26 "language": "JA", 27 "content": ('"'+v.text+'"') 28 }, 29 "encodingType": "UTF8" 30 } 31 response = requests.post(url, headers=header, json=body).json() 32 33 db.sample21.insert(response) 34 35 if __name__ == '__main__': 36 main("".join(sys.argv[1:]), "アクセストークン(実際のアクセストークンが入ります)") 37
あなたの回答
tips
プレビュー