mysql - Python global name 'urls' is not defined Issue -


though urls defined, keep getting "global name 'urls' not defined" , url data not inserted mysql. suggestions on where? i'm making mistake here?

# ! /usr/bin/python  # description : script can collect urls tweets , records them research mysql db.  __future__ import print_function import tweepy import json import mysqldb dateutil import parser  words = ['security']  # credentails consumer_key = "" consumer_secret = "" access_token = "" access_token_secret = ""  host = "192.168.150.94" user = "root" passwd = "blah" database = "tweets"   def store_data(tweet_url):     db = mysqldb.connect(host=host, user=user, passwd=passwd, db=database,                          charset="utf8")     cursor = db.cursor()     insert_query = "insert  tweet_url (urls) values (%s)"     cursor.execute(insert_query, (urls))     db.commit()     cursor.close()     db.close()     return   class streamlistener(tweepy.streamlistener):     def on_connect(self):         print("we connected streaming api.")      def on_error(self, status_code):         print('an error has occured: ' + repr(status_code))         return false      def on_data(self, data):         try:             datajson = json.loads(data)             web_url = datajson['entities']['urls']             print(web_url)             in web_url:                 web_urls = i['expanded_url']                 urls = web_urls             print(urls)         store_data(urls)      except exception e:     print(e)   auth = tweepy.oauthhandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) 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) 

you need rename parameter urls in function store_data tweet_url

def store_data(tweet_url):     db = mysqldb.connect(host=host, user=user, passwd=passwd, db=database,                          charset="utf8")     cursor = db.cursor()     insert_query = "insert  tweet_url (urls) values (%s)"     cursor.execute(insert_query, (tweet_url)) 

the way want store data stays unclear. if call store_data after loop, it's storing last value, should better store each value in list:

def on_data(self, data):     try:         datajson = json.loads(data)         web_url = datajson['entities']['urls']         print(web_url)         urls = []         in web_url:             urls.append((i['expanded_url'],))              # stores tuple make easy in database insertion         print(urls)         store_data(urls)     except:          [...] 

this way need little fix inside store_data:

def store_data(urls):     db = mysqldb.connect(host=host, user=user, passwd=passwd, db=database,                          charset="utf8")     cursor = db.cursor()     insert_query = "insert  tweet_url (urls) values (%s)"     cursor.executemany(insert_query, urls)     db.commit()     cursor.close()     db.close()     return 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -