我正在使用twitter的api“ tweepy”,希望从tweet中抓取可用的地理位置坐标。我的最终目标是将每个推文的坐标仅存储在表中。
我的问题是,当位置鸣叫时,我遇到了一个错误,该错误提供了比坐标更多的信息:
到目前为止,我的代码如下:
import pandas as pd
import json
import tweepy
import csv
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
if status.retweeted:
return
if True:
coords = status.coordinates
geo = status.geo
if geo is not None:
geo = json.dumps(geo)
if coords is not None:
coords = json.dumps(coords)
print(coords, geo)
with open('coordinates_data.csv', 'a') as f:
writer = csv.writer(f)
writer.writerow([coords,geo])
def on_error(self, status_code):
if status_code == 420:
#returning False in on_error disconnects the stream
return False
LOCATIONS = [-124.7771694, 24.520833, -66.947028, 49.384472, # Contiguous US
-164.639405, 58.806859, -144.152365, 71.76871, # Alaska
-160.161542, 18.776344, -154.641396, 22.878623] # Hawaii
auth = tweepy.OAuthHandler('access auths, 'access auths')
auth.set_access_token('token','token')
api = tweepy.API(auth)
user = api.me()
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener=myStreamListener)
我确定这个问题与我对'json'的缺乏了解,或者我需要使用数据字典有关。
我将不胜感激!
为了澄清,Tweepy是与Twitter的API交互的第三方库。
这就是Twitter代表coordinates objects的方式。 Tweepy将coordinates
/ Status
数据的Tweet object属性解析为字典。您可以简单地访问coordinates
字段作为该词典的关键字,以获取包含经度和纬度值的列表。
您在初始化'
的地方也缺少引号auth
,但是我认为这是您替换此问题的凭据时出现的错字。