0
私が使用しているデータは、Twitter APIのtwitter動向のトピックです。jsonリストをPythonの辞書に変換する
url_0 = 'https://api.twitter.com/1.1/trends/place.json?id=2459115'
res = requests.get(url_0, auth=auth)
print(res, res.status_code, res.headers['content-type'])
print(res.url)
top_trends_twitter = res.json()
data= top_trends_twitter[0]
これは、データがどのように見えるかです:私は、SQLを使用してサーバーに接続し、データベースとテーブルを作成した後
今[{'as_of': '2017-02-13T21:59:32Z',
'created_at': '2017-02-13T21:53:22Z',
'locations': [{'name': 'New York', 'woeid': 2459115}],
'trends': [{'name': 'Victor Cruz',
'promoted_content': None,
'query': '%22Victor+Cruz%22',
'tweet_volume': 45690,
'url': 'http://twitter.com/search?q=%22Victor+Cruz%22'},
{'name': '#percussion',
'promoted_content': None,
'query': '%23percussion',
'tweet_volume': None,
'url': 'http://twitter.com/search?q=%23percussion'}, .....etc
、エラーが表示されます。これは私のトラブルの原因となっている部分である。そして、
for entry in data:
trendname = entry['trends']['name']
url = entry['trends']['url']
num_tweets = entry['trends']['trend_volume']
date= entry['as_of']
print("Inserting trend", trendname, "at", url)
query_parameters = (trendname, url, num_tweets, date)
cursor.execute(query_template, query_parameters)
con.commit()
cursor.close()
を、私はこのエラーを取得する:
TypeError Traceback (most recent call last)
<ipython-input-112-da3e17aadce0> in <module>()
29
30 for entry in data:
---> 31 trendname = entry['trends']['name']
32 url = entry['trends']['url']
33 num_tweets = entry['trends']['trend_volume']
はTypeError:文字列のインデックスは整数
どのようにする必要があり私は辞書の中に文字列の集合を持っているので、それを入力データコードに使うことができますか?
はまだ同じ問題を取得...私が行うときにデータ[ 'as_of']、私は、値を取得んが、ときに私がそのコードを実行すると、date = entry ['as_of']を指す同じエラーが発生します。 – Elizabeth
@Elizabethコードはテストされていますが、質問で提供したデータのために機能しています。私は 'data = res.json()'を試してみるべきだと思います。それは私が願っています。 – MYGz
その作業!!!本当にありがとう – Elizabeth