我正在使用Tweepy访问流式API。我可以用下面的代码得到结果,但是对于Geo-Enabled值为“True”的tweets,我得到的是坐标返回值“False”。怎么会这样?是否需要解码status.coordinates返回的JSON对象?
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import random
import time
import MySQLdb
import json
consumer_key="XXX"
consumer_secret="XXX"
access_token="XXX"
access_token_secret="XXX"
db=MySQLdb.connect(host='localhost', user='XXX', passwd='XXX', db='twitter')
db.set_character_set('utf8')
Coords = dict()
Place = dict()
PlaceCoords = dict()
XY = []
curr=db.cursor()
class StdOutListener(StreamListener):
""" A listener handles tweets that are the received from the stream.
This is a basic listener that inserts tweets into MySQLdb.
"""
def on_status(self, status):
print "Tweet Text: ",status.text
text = status.text
print "Time Stamp: ",status.created_at
print "Time Stamp: ",status.created_at
print "Source: ",status.source
source = status.source
print "Author: ",status.user.screen_name
author = status.user.screen_name
print "Name: ",status.user.name
name = status.user.name
print "Time Zone: ",status.user.time_zone
time_zone = status.user.time_zone
print "User Language: ",status.user.lang
user_language = status.user.lang
print "Followers: ",status.user.followers_count
followers = status.user.followers_count
print "User Description: ",status.user.description
user_description = status.user.description
print "Geo Enabled: ",status.user.geo_enabled
geo_enabled = status.user.geo_enabled
print "Friends: ",status.user.friends_count
friends = status.user.friends_count
print "Retweets: ",status.retweet_count
retweets = status.retweet_count
print "Location: ",status.user.location
location = status.user.location
print "ID: ",status.user.id_str
user_id = status.user.id_str
print "Coordinates: ",status.coordinates
coordinates = status.coordinates
print "Place: ",status.place
place = status.place
下面是一个示例结果输出:
Tweet Text: @aranone aran tu eres el mejor soy tu fanatico 1 me gusta tu musica.hey pana sique asi q vay bn te deseo lo mejor bro)
Time Stamp: 2013-05-30 23:36:38
Time Stamp: 2013-05-30 23:36:38
Source: web
Author: juandvd_96
Name: juan David Romero
Time Zone: Atlantic Time (Canada)
User Language: es
Followers: 365
User Description: hola soy juan david... soy una chico muy enamorado... y soy muy fekiz...
Geo Enabled: True
Friends: 1857
Retweets: 0
Location: veezuela maracaibo
ID: 481513551
Coordinates: None
Place: None
干杯, BD公司
谢谢你的澄清。我刚才正在检查侦听器,注意到一条tweet,其中填充了坐标,但它是一个json对象。我正在将tweets写入mysql数据库,因为它们是流式的,而且似乎带有坐标信息的tweets没有插入到数据库中。不确定SQL语句周围的错误是针对第一条tweet还是第二条tweet,发生错误的两列都设置为“varchar”值。以下是流结果:
Tweet文本:Vi 10分钟,没有pude ver mas。大豆超级卡戈纳,迪奥斯。我不知道。
时间戳:2013-06-04 01:08:57
时间戳:2013-06-04 01:08:57
来源:网络
作者:艾伦瓦利
名称:∧ili
时区:圣地亚哥
用户语言:es
追随者:384
用户描述:创建您的现实,否则它将为您创建
http://instagram.com/ailenvalli
地理启用:真
朋友:338
转发:0
地点:东百老汇704号¨1966
编号:200264965
坐标:无
地点:无
firehose_geo.py:87:警告:字符串值不正确:第1行“Name”列的“\xCE\x9Bili”
(文本,状态。已创建,状态。已创建,源,作者,名称,时区,用户语言,关注者,用户描述,启用地理位置,好友,转发,位置,用户id,坐标,地理位置) firehose_geo.py:87:警告:不正确的字符串值:'\xE2\x96\xB2 19…'对于第1行的列'Location'
(文本,状态。已创建,状态。已创建,源,作者,名称,时区,用户语言,关注者,用户描述,启用地理位置,好友,转发,位置,用户id,坐标,地理位置)
推特短信:我有种感觉,沃尔玛打算从我的钱包里拿出一大块。健康食品太贵了。
时间戳:2013-06-04 01:42:00
时间戳:2013-06-04 01:42:00
来源:Android版Twitter
作者:KaylaRenae21
姓名:Kayla Renae
时区:中部时间(美国和加拿大)
用户语言:英语
追随者:300人
用户描述:城市里找不到我喜欢做的事情。给我一根钓鱼竿,我一整天都不在。
地理启用:真
朋友:437
转发:0
地点:俄克拉荷马州
编号:282414509
坐标:{'type':'Point','Coordinates':[-96.6623549,34.7918959]}
地点:{'type':'Point','coordinates':[34.7918959,-96.6623549]}
这个问题与
tweepy
本身无关。例如,请参阅此tweet(https://api.twitter.com/1/statuses/show.json?id=341458303064354817&include_entities=true)-它将
geo_enabled
设置为true,而geo
、coordinates
和place
等于null
。根据twitter docs:
所以,如果
geo_enabled
是真的,那么在tweet数据中就有位置信息并不是一个严格的规则。只要检查一下status.geo
或status.coordinates
在您的侦听器中是否是not None
。希望能有所帮助。
相关问题 更多 >
编程相关推荐