简体   繁体   English

使用Python从推文中提取用户信息

[英]Extracting user information from tweets using Python

I am trying to extract some user information from the tweets I have downloaded. 我正在尝试从下载的推文中提取一些用户信息。 Below is the code that I am using, however, it only returns "None" in the list for all the tweets. 下面是我正在使用的代码,但是,对于所有tweet,它在列表中仅返回“ None”。

map(lambda test: test['place']['country'] , data)

Also, tried the following:- 另外,尝试了以下方法:

map(lambda test: test.get('place').get('country'), data)

I am relatively new to Python. 我对Python比较陌生。 It will be really helpful if someone can please help me understand if I am missing something here. 如果有人可以帮助我了解我在这里缺少什么,那将真的很有帮助。

This will just be due to the fact that the vast majority of tweets do not seem to carry the 'place' data - Its nearly always set to None. 这仅是由于以下事实:绝大多数推文似乎都不携带“位置”数据-它几乎总是设置为“无”。

Your best bet for a location would be to use the location key inside the 'user' section of the data dump - but even that isnt always filled out (You would have to try/except to handle TypeErrors for None type objects. See below for what I use within my listener. 最好的位置选择是在数据转储的“用户”部分中使用位置键-但即使这样也不总是被填写(您必须尝试/除非要为None类型对象处理TypeErrors。请参见下文)我在听众中使用的东西。

def on_data(self, data):
    tweet = json.loads(data)
    print "\n\n ***********************************"
    try:
        print tweet['created_at'][:19] + ' - ' + tweet["text"] + ' - ' + tweet['user']['location']
    except TypeError:
        print tweet['created_at'][:19] + ' - ' + tweet["text"] + ' - ' 'No Location Given'
    return True

Hope this helps you on your way! 希望这对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM