【问题标题】:Unable to extract coordinates from twitter API无法从 twitter API 中提取坐标
【发布时间】:2021-01-30 09:45:14
【问题描述】:

这是我的代码:

[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]

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

谁能帮我解决这个问题,因为没有经纬度数据我无法继续进行。

提前谢谢你...

【问题讨论】:

  • 为什么你的第一次导入中有一个“[”?
  • @jps 这是一个网址,但不可点击。

标签: python api export-to-csv tweepy twitterapi-python


【解决方案1】:

位置对象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...

【讨论】:

  • 感谢您的建议。但是有没有其他方法可以得到推文的经纬度?因为我要进一步把这些数据放到强制要求坐标数据的GIS平台中。
  • 似乎不是这样:推文必须先由用户标记,然后才能包含 Place 有效负载
猜你喜欢
  • 2020-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 2016-05-15
  • 1970-01-01
  • 2019-02-19
  • 2013-06-14
  • 2021-05-25
相关资源
最近更新 更多