解析提取用户位置的用户名Twitter

时间:2016-07-22 10:24:49

标签: python pandas twitter geolocation latitude-longitude

我正试图从twitter上抓取用户名的用户位置。

输入:用户列表的用户名超过50K

AkkiPritam,6.77E+17,12/15/2015,#chennaifloods
AkkiPritam,6.77E+17,12/15/2015,#bhoomikatrust
AkkiPritam,6.77E+17,12/15/2015,#akshaykumar
gischethans,6.77E+17,12/15/2015,#chennaifloods
mid_day,6.77E+17,12/15/2015,#bollywood
mid_day,6.77E+17,12/15/2015,#chennaifloods
Nanthivarman16,6.77E+17,12/15/2015,#admkfails
Nanthivarman16,6.77E+17,12/15/2015,#jayafails
Nanthivarman16,6.77E+17,12/15/2015,#stickergovt
Nanthivarman16,6.77E+17,12/15/2015,#chennaifloods
AdilaMatra,6.77E+17,12/15/2015,#chennaifloods
AdilaMatra,6.77E+17,12/15/2015,#climatechange
AdilaMatra,6.77E+17,12/15/2015,#delhichokes
AdilaMatra,6.77E+17,12/15/2015,#smog
HDFCERGOGIC,6.77E+17,12/15/2015,#chennaifloods
HDFCERGOGIC,6.77E+17,12/15/2015,#tnfloods
ImSoorej,6.77E+17,12/15/2015,#chennaifloods
ImSoorej,6.77E+17,12/15/2015,#chennaimicr

代码:我想找到地理位置可能的地理坐标。

from __future__ import print_function
import tweepy
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
import pandas as pd
import csv

consumer_key = 'xyz'
consumer_secret = 'xyz'
access_token = 'xyz'
access_token_secret = 'xyz'

data = pd.read_csv('user_keyword.csv')
df = ['user_name', 'user_id', 'date', 'keyword']

def get_user_details(username):
        userobj = api.get_user(username)
        return userobj

if __name__ == '__main__':
    #authenticating the app (https://apps.twitter.com/)
    auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)

    username = df['user_name']
    userOBJ = get_user_details(username)
    print(userOBJ.location)

错误:无法将用户名解析为程序。

Traceback (most recent call last):
  File "user_profile_location.py", line 38, in <module>
    username = df['user_name']
TypeError: list indices must be integers, not str

1 个答案:

答案 0 :(得分:1)

您正在使用'数据'来定义您的DataFrame,而'df'用于我认为应该是DataFrame的列

data = pd.read_csv('user_keyword.csv')
df = ['user_name', 'user_id', 'date', 'keyword']

我假设user_keyword.csv文件没有标题,请尝试添加:

data.columns = df

它会将列名更改为存储在df中的值。 然后代替:

username = df['user_name']

尝试:

username = data['user_name']

请注意,现在用户名是整列,因此get_user_details(username)不应该只需要一个字符串。