无法从 Twitter API 中提取坐标

问题描述

这是我的代码:

[import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import pandas as pd
import json
import csv
import sys
import time

#reload(sys)
#sys.setdefaultencoding('utf8')

ckey = "xxxxxxxxxxxxx"
csecret = "xxxxxxxxxxxx"
atoken = "xxxxxxxxxxxx"
asecret = "xxxxxxxxxx"

def toDataFrame(tweets):
    # COnvert to data frame
    DataSet = pd.DataFrame()

    DataSet\['tweetID'\] = \[tweet.id for tweet in tweets\]
    DataSet\['tweetText'\] = \[tweet.text.encode('utf-8') for tweet in tweets\]
    DataSet\['tweetRetweetCt'\] = \[tweet.retweet_count for tweet in tweets\]
    DataSet\['tweetFavoriteCt'\] = \[tweet.favorite_count for tweet in tweets\]
    DataSet\['tweetSource'\] = \[tweet.source for tweet in tweets\]
    DataSet\['tweetCreated'\] = \[tweet.created_at for tweet in tweets\]
    DataSet\['userID'\] = \[tweet.user.id for tweet in tweets\]
    DataSet\['userScreen'\] = \[tweet.user.screen_name for tweet in tweets\]
    DataSet\['userName'\] = \[tweet.user.name for tweet in tweets\]
    DataSet\['userCreateDt'\] = \[tweet.user.created_at for tweet in tweets\]
    DataSet\['userDesc'\] = \[tweet.user.description for tweet in tweets\]
    DataSet\['userFollowerCt'\] = \[tweet.user.followers_count for tweet in tweets\]
    DataSet\['userFriendsCt'\] = \[tweet.user.friends_count for tweet in tweets\]
    DataSet\['userLocation'\] = \[tweet.user.location for tweet in tweets\]
    DataSet\['userTimezone'\] = \[tweet.user.time_zone for tweet in tweets\]
    DataSet\['Coordinates'\] = \[tweet.coordinates for tweet in tweets\]
    DataSet\['GeoEnabled'\] = \[tweet.user.geo_enabled for tweet in tweets\]
    DataSet\['Language'\] = \[tweet.user.lang for tweet in tweets\]
    tweets_place= \[\]
    #users_retweeted = \[\]
    for tweet in tweets:
        if tweet.place:
            tweets_place.append(tweet.place.full_name)
        else:
            tweets_place.append('null')
    DataSet\['TweetPlace'\] = \[i for i in tweets_place\]
    #DataSet\['UserWhoRetweeted'\] = \[i for i in users_retweeted\]

    return DataSet

OAUTH_KEYS = {'consumer_key':ckey,'consumer_secret':csecret,'access_token_key':atoken,'access_token_secret':asecret}
#auth = tweepy.OAuthHandler(OAUTH_KEYS\['consumer_key'\],OAUTH_KEYS\['consumer_secret'\])
auth = tweepy.AppAuthHandler(OAUTH_KEYS\['consumer_key'\],OAUTH_KEYS\['consumer_secret'\])

api = tweepy.API(auth,wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
if (not api):
    print ("Can't Authenticate")
    sys.exit(-1)
else:
    print ("Scraping data now") # Enter lat and long and radius in Kms  q='ganesh'
    cursor = tweepy.Cursor(api.search,q="travel",since='2021-01-27',until='2021-01-28',lang='en',count=50)
    results=\[\]
    for item in cursor.items(1000): # Remove the limit to 1000
            results.append(item)


    DataSet = toDataFrame(results)
    DataSet.to_csv('Result.csv',index=False)
    print ("Completed.. !!")][1]

[click here to see the output image.][1]
[1]: https://i.stack.imgur.com/Ih67w.png

我正在尝试使用 API 获取 Twitter 数据。为了我的研究,我需要推文的坐标,即纬度和经度。在输出中,坐标为空。

有人可以帮我解决这个问题,因为没有经纬度数据我就无法继续。

先谢谢你...

解决方法

位置对象 coordinates 已提供但通常为空,这是由于 Twitter 前段时间所做的更改。

您应该依赖 Place 对象,该对象提供地点名称和坐标如果用户分享位置或向推文添加地点。

'place': {'id': '07d9edfbc6085000','url': 'https://api.twitter.com/1.1/geo/id/07d9edfbc6085000.json','place_type': 'poi','name': 'The place','country': 'The Netherlands','bounding_box': 
   {'type': 'Polygon','
     coordinates': [[[4.853216261144857,52.36157762209008]
     etc...

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...