如何解决来自 Tweet 的 Python Tweepy 打印国家/地区
我正在玩 Tweepy 查询推文。但是我无法在推文中打印出该国家/地区。 (如果有)见下面的代码。
import mysql.connector
from mysql.connector import errors
import tweepy
import json
from dateutil import parser
import time
import os
print("Tweeter app started")
# Importing file which sets environment variables
with open('twitter_credentials.json') as api:
data = json.load(api)
#print(data) # testing json file
consumer_key = data['CONSUMER_KEY']
consumer_secret = data['CONSUMER_SECRET']
access_token = data['ACCESS_TOKEN']
access_token_secret = data['ACCESS_SECRET']
if __name__ == '__main__':
track = []
while True:
input1 = input("What do you want to collect tweets on?: ")
track.append(input1)
input2 = input("Do you wish to enter another word? y/n: ")
if input2 == 'n' or input2 == 'N':
break
print("You want to search for{}".format(track))
print("Initialising Connection to Twitter API....")
time.sleep(2)
# Authentication so we can access Twitter
auth = tweepy.auth.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True)
search_results = api.search(q=track,count=100)
for i in search_results:
print(i)
print(i.author._json['screen_name'])
print(parser.parse(i.author._json['created_at']))
print(i.text)
print(i.retweet_count)
print(i.author._json['location'])
print(i.coordinates)
if hasattr(i.place,'Country'):
print('COUNTRY :' + i.place,'Country')
else:
print("null")
如您所见,我有 hasattr(i.place,'Country'),我只需要从推文的 json 格式中知道要使用的正确路径。
亲切的问候
罗布
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。