2017-01-26 2 views
1

J'utilise ce script pour diffuser des tweets de Twitter en utilisant tweepy et je suis confronté à un problème avec le paramètre de coordonnées.comment stocker les coordonnées streaming forme tweepy api dans mysql db?

chaque fois que je reçois un tweet avec les coordonnées, je reçois cette erreur:

(1064, ‘You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \’: “\’Point\'”, u\’coordinates\’: \'(28.5383355,-81.3792365)\’})\’ at line 1′) 

aussi, mon état coordonnées pour stocker uniquement les tweets avec géo-localisation ne prend pas effet. Tous les tweets entrants semblent être stockés dans le db.

import tweepy 
import json 
import MySQLdb 
from dateutil import parser 

WORDS = ['#bigdata', '#AI', '#datascience', '#machinelearning', '#ml', '#iot'] 

CONSUMER_KEY = "" 
CONSUMER_SECRET = "" 
ACCESS_TOKEN = "" 
ACCESS_TOKEN_SECRET = "" 

HOST = "" 
USER = "" 
PASSWD = "" 
DATABASE = "" 

# This function takes the 'created_at', 'text', 'screen_name', 'tweet_id' and 'coordinates' and stores it 
# into a MySQL database 
def store_data(created_at, text, screen_name, tweet_id, coordinates): 
    db=MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DATABASE, charset="utf8") 
    cursor = db.cursor() 
    insert_query = "INSERT INTO twitter (tweet_id, screen_name, created_at, text, coordinates) VALUES (%s, %s, %s, %s, %s)" 
    cursor.execute(insert_query, (tweet_id, screen_name, created_at, text, coordinates)) 
    db.commit() 
    cursor.close() 
    db.close() 
    return 

class StreamListener(tweepy.StreamListener): 
    #This is a class provided by tweepy to access the Twitter Streaming API. 

    def on_connect(self): 
     # Called initially to connect to the Streaming API 
     print("You are now connected to the streaming API.") 

    def on_error(self, status_code): 
     # On error - if an error occurs, display the error/status code 
     print('An Error has occured: ' + repr(status_code)) 
     return False 

    def on_data(self, data): 
     #This is the meat of the script...it connects to your mongoDB and stores the tweet 
     try: 
      # Decode the JSON from Twitter 
      datajson = json.loads(data) 

      if datajson['coordinates']=='None': 
       print 'coordinates = None, skipped' 

      else: 

      #grab the wanted data from the Tweet 
       text = datajson['text'] 
       screen_name = datajson['user']['screen_name'] 
       tweet_id = datajson['id'] 
       created_at = parser.parse(datajson['created_at']) 
       coordinates = datajson['coordinates'] 

       #print out a message to the screen that we have collected a tweet 
       print("Tweet collected at " + str(created_at)) 
       #print datajson 
       #insert the data into the MySQL database 
       store_data(created_at, text, screen_name, tweet_id, coordinates) 

     except Exception as e: 
      print(e) 
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) 
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) 
#Set up the listener. The 'wait_on_rate_limit=True' is needed to help with Twitter API rate limiting. 
listener = StreamListener(api=tweepy.API(wait_on_rate_limit=True)) 
streamer = tweepy.Stream(auth=auth, listener=listener) 
print("Tracking: " + str(WORDS)) 
streamer.filter(track=WORDS) 

Répondre

0

erreur SQL

le résultat de tweepy pour les coordonnées pas un nombre ou une chaîne, la place d'un objet.

vous devez analyser cet objet dans le lan & lon et enregistrer chacun dans une colonne différente.

pour obtenir la latitude et la longitude au lieu de cette ligne:

coordinates = datajson['coordinates'] 

faire:

latitude, longitude = datajson["coordinates"]["coordinates"] 

aussi, mon état coordonnées pour stocker uniquement les tweets avec géo-localisation ne prend pas effet. Tous les tweets entrants semblent être stockés dans le db.

'None' est une chaîne et non la variable None

remplacer:

if datajson['coordinates']=='None': 

avec:

if datajson['coordinates'] is None: 

ou mieux:

if not datajson['coordinates']: 
+0

ok, la condition travaillée. pouvez-vous expliquer plus comment résoudre l'erreur sql? – haytham

+0

modifié espérons que cela clarifier plus – ShmulikA

+0

quand j'ai ajouté la latitude, longitude = datajson ["coordonnées"] ["coordonnées"], j'ai eu cette erreur: objet 'NoneType' n'a pas d'attribut '__getitem__' – haytham