我成功地从Twitter API提取了推文,直到我决定将密钥/令牌放在单独的配置文件中。当我计划将主文件上传到Github时。 到目前为止,我发现的关于StackOverflow的解决方案并没有解决我的问题。
import oauth2 as oauth
import json
import configparser
config = configparser.RawConfigParser()
configpath = r'config.py'
config.read(configpath)
consumer_key = config.get('logintwitter', 'consumer_key')
consumer_secret = config.get('logintwitter', 'consumer_secret')
access_key = config.get('logintwitter', 'access_key')
access_secret = config.get('logintwitter', 'access_secret')
consumer = oauth.Consumer(key=consumer_key, secret=consumer_secret) #twitter: sign me in
access_token = oauth.Token(key=access_key, secret=access_secret) #grant me access
client = oauth.Client(consumer, access_token) #object return
timeline_endpoint = "https://api.twitter.com/1.1/statuses/home_timeline.json"
response, data = client.request(timeline_endpoint)
tweets = json.loads(data) #take a JSON string convert it to dictionary structure:
for tweet in tweets:
print(tweet["text"])
这是错误消息:
回溯(最近通话最近):
文件 “ /Users/myname/PycharmProjects/twiiter2/twitterconnect.py”,第24行, 在 print(tweet [“ text”])TypeError:字符串索引必须为整数
我尝试更改json.loads()方法以及print(tweet [“ text”])中的内容
谦虚地为我指出正确的方向。
谢谢!