[英]Tweepy Streaming API returning “None” for coordinates on geo-enabled tweets
我正在使用Tweepy訪問流API。 我可以使用以下代碼獲得結果,但是對於地理啟用值為“ 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
姓名:胡安·大衛·羅梅羅
時區:大西洋時間(加拿大)
用戶語言:es
粉絲365
用戶描述:hola soy juan david ... soy una chico muy enamorado ... y soy muy fekiz ...
啟用地理:True
好友:1857
轉推:0
所在地:veezuela maracaibo
ID(標識號):481513551
坐標:無
地點:無
歡呼聲,BD
感謝您的澄清。 我剛才正在檢查偵聽器,並注意到一條鳴叫,其中填充了坐標,但作為json對象。 我正在將tweet寫到mysql db,因為它們流式傳輸,並且似乎沒有將具有坐標信息的tweet插入數據庫。 不確定SQL語句周圍的錯誤是針對第一條推文還是第二條推文,發生錯誤的兩列均設置為“ varchar”值。 這是流式傳輸結果:
推文:Vi 10分鍾和否。 大豆超級卡岡,迪奧斯。 反對。
時間戳記:2013-06-04 01:08:57
時間戳記:2013-06-04 01:08:57
資料來源:網站
作者:ailenvalli
姓名:Λili
時區:聖地亞哥
用戶語言:es
粉絲384
用戶描述:創建您的現實,否則它將為您創建
http://instagram.com/ailenvalli
啟用地理:True
朋友338
轉推:0
位置:東百老匯704號▲1966
ID(標識號):200264965
坐標:無
地點:無
firehose_geo.py:87:警告:第1行“名稱”列的字符串值不正確:“ \\ xCE \\ x9Bili”
(文本,status.created_at,status.created_at,源,作者,名稱,時區,用戶語言,關注者,用戶描述,啟用了地理位置,朋友,轉發,位置,user_id,坐標,geo))firehose_geo.py:87:警告:不正確的字符串值:第1行“位置”列的“ \\ xE2 \\ x96 \\ xB2 19 ...”
(文字,status.created_at,status.created_at,出處,作者,名稱,TIME_ZONE,USER_LANGUAGE,追隨者,USER_DESCRIPTION,geo_enabled,朋友,銳推,地理位置優越,USER_ID,坐標,GEO))
推文:我覺得沃爾瑪正在修理要從我的錢包里拿出一大塊錢。 健康食品是如此昂貴。
時間戳記:2013-06-04 01:42:00
時間戳記:2013-06-04 01:42:00
資料來源:Android版Twitter
作者:KaylaRenae21
姓名:†Kayla Renae'
時區:中部時間(美國和加拿大)
用戶語言:zh
粉絲300
用戶描述:我喜歡做的事情在城市中找不到。 給我一根釣魚竿,我會整天走了。
啟用地理:True
好友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
為true,則在tweet數據中必須包含位置信息並不是嚴格的規則。 只需檢查status.geo
或status.coordinates
是否not None
。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.