解析提取用户位置Twitter的用户名 [英] Parse User name for extracting user location Twitter
问题描述
我试图从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.77 E + 17,12 / 15/2015,#烟雾
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
从tweepy导入tweepy
从tweepy导入OAuthHandler
import Stream
from tweepy.streaming import StreamListener
导入熊猫作为pd
导入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','日期','关键字']
def get_user_details(用户名):
userobj = api.get_user(用户名)
返回userobj
if __name__ = ='__main__':
#authenticating 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(最近一次调用最后一次):
在< module>>中的第38行的user_profile_location.py ;
username = df ['user_name']
TypeError:列表索引必须是整数,而不是str
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']
$ c $username = data [ 'user_name']
请记住,现在用户名是整列,所以
get_user_details(用户名)
不应期待单个字符串。I am trying to scrape user location with respect to user names from twitter.
Input: The user list has more than 50K User names
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
Code: I want to find geo location possibly geo coordinates.
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)
Error: Trouble parsing the usernames into program.
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
解决方案You are using 'data' to define your DataFrame and 'df' for what I think should be the columns of the DataFrame
data = pd.read_csv('user_keyword.csv') df = ['user_name', 'user_id', 'date', 'keyword']
I assume that the user_keyword.csv file has no header, try adding:
data.columns = df
It will change the column names to the values stored in df. Then later instead of:
username = df['user_name']
Try:
username = data['user_name']
Keep in mind that now username is a whole column so
get_user_details(username)
should not be expecting a single string.这篇关于解析提取用户位置Twitter的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文