use*_*175 2 python twitter tweepy twitter-streaming-api
我正在使用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
Run Code Online (Sandbox Code Playgroud)
这是一个示例结果输出:
推文:@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
姓名:胡安大卫罗梅罗
时区:大西洋时间(加拿大)
用户语言:es
粉丝:365
用户说明:hola soy juan david ...大豆una chico muy enamorado ... y soy muy fekiz ...
地理位置已启用:正确
朋友们: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
地理位置已启用:正确
朋友:338
转推:0
地点:704 East Broadway▲1966
ID:200264965
坐标:无
地点:无
firehose_geo.py:87:警告:字符串值不正确:第1行第'Name'列的'\ xCE\x9Bili'
(text,status.created_at,status.created_at,source,author,name,time_zone,user_language,followers,user_description,geo_enabled,friends,retweets,location,user_id,coordinates,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))
Tweet Text:我有一种感觉,WalMart正在修理我的钱包.健康的食物是如此昂贵.
时间戳:2013-06-04 01:42:00
时间戳:2013-06-04 01:42:00
来源:Android推特
作者:KaylaRenae21
姓名:†Kayla Renae'
时区:中部时间(美国和加拿大)
用户语言:en
粉丝:300
用户说明:在城市中找不到我喜欢做的事情.递给我一根钓鱼竿,我一整天都会离开.
地理位置已启用:正确
朋友:437
转推:0
地点:俄克拉荷马州
ID:282414509
坐标:{'type':'Point','coordinates':[ - 96.6623549,34.7918959]}
地点:{'type':'Point','coordinates':[34.7918959,-96.6623549]}
问题与tweepy自身无关.
例如,看到这种鸣叫(https://api.twitter.com/1/statuses/show.json?id=341458303064354817&include_entities=true) -它已geo_enabled设置为true,同时geo,coordinates和place等于null.
根据twitter文档:
geo_enabled:如果为true,则表示用户已启用对其推文进行地理标记的可能性.
因此,如果geo_enabled是真的,那么推文数据中将存在位置信息并不是一个严格的规则.只是检查status.geo或者status.coordinates是not None在你的听众.
希望有所帮助.