我正在使用Tweepy访问流API。我可以使用下面的代码获得结果,但对于Geo Enabled值为“True”的推文,我得到的坐标返回值为“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
以下是示例结果输出:
推文:@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)
时间戳:2013-05-30 23:36:38
时间戳:2013-05-30 23:36:38
来源:网站
作者:juandvd_96
姓名:juan David Romero
时区:大西洋时间(加拿大)
用户语言:es
粉丝:365
用户说明:hola soy juan david ...大豆una chico muy enamorado ... y soy muy fekiz ...
Geo Enabled:True
朋友:1857
转推:0
地点:veezuela maracaibo
ID:481513551
坐标:无
地点:无
欢呼声, BD
感谢您的澄清。我刚刚检查了听众,并发现了一条推文,其中填充了坐标但是作为json对象。我正在向mysql db发送推文,因为它们是流式传输的,似乎没有将带有坐标信息的数据库插入到数据库中。不确定SQL语句周围的错误是针对第一条推文还是第二条推文,发生错误的两列都设置为“varchar”值。以下是流式传输结果:
推文:Vi 10 minutos y no pude ver mas。大豆超级cagona,dios。 Vay a ver otra。
时间戳:2013-06-04 01:08:57
时间戳:2013-06-04 01:08:57
来源:网站
作者:ailenvalli
姓名:Λili
时区:圣地亚哥
用户语言:es
粉丝:384
用户描述:创建您的现实或将为您创建
http://instagram.com/ailenvalli
Geo Enabled:True
朋友:338
转推:0
地点:704 East Broadway▲1966
ID:200264965
坐标:无
地点:无
firehose_geo.py:87:警告:字符串值不正确:第1行“名称”列的'\ xCE \ x9Bili'
(文本,status.created_at,status.created_at,源,作者姓名,的time_zone,USER_LANGUAGE,追踪者USER_DESCRIPTION,geo_enabled,朋友,锐推,位置,USER_ID,坐标,GEO)) firehose_geo.py:87:警告:字符串值不正确:'\ xE2 \ x96 \ xB2 19 ...'代表第1行的“位置”列
(文本,status.created_at,status.created_at,源,作者姓名,的time_zone,USER_LANGUAGE,追踪者USER_DESCRIPTION,geo_enabled,朋友,锐推,位置,USER_ID,坐标,GEO))
推文:我有一种感觉,WalMart正在修理我的钱包。健康的食物非常昂贵。
时间戳:2013-06-04 01:42:00
时间戳:2013-06-04 01:42:00
来源:Twitter for Android
作者:KaylaRenae21
姓名:†Kayla Renae'
时区:中部时间(美国和加拿大)
用户语言:en
粉丝:300
用户描述:在城市中找不到我喜欢做的事情。递给我一根钓鱼竿和钓鱼竿。我一整天都会离开。
Geo Enabled:True
朋友:437
转推:0
地点:俄克拉荷马州
ID:282414509
坐标:{'type':'Point','coordinates':[ - 96.6623549,34.7918959]}
地点:{'type':'Point','coordinates':[34.7918959,-96.6623549]}
答案 0 :(得分:5)
问题与tweepy
本身无关。
例如,请参阅此推文(https://api.twitter.com/1/statuses/show.json?id=341458303064354817&include_entities=true) - geo_enabled
设置为geo
,coordinates
和place
等于{{1} }}
根据twitter docs:
geo_enabled:如果为true,表示用户已启用 对他们的推文进行地理标记的可能性。
因此,如果null
为真,那么推文数据中会有位置信息并不严格。只需检查听众中geo_enabled
或status.geo
是status.coordinates
。
希望有所帮助。