0

首先,以下是我的代码:

#################################################################
#PART I: import "twitter_handle.csv" file into SQL DB
#################################################################

# the name of the SQL DB table is TWITTER_HANDLE  
import csv
import sqlite3 

# Create the database
connection = sqlite3.connect(':memory:')
cursor = connection.cursor()
# Create the table
cursor.execute('DROP TABLE IF EXISTS TWITTER_HANDLE')
cursor.execute('CREATE TABLE TWITTER_HANDLE (twitter_handle TEXT) ')
connection.commit()
# convert twitter_handle.csv file from the desktop to tab-deliminted file
with open('/Users/hyunjincho/Desktop/twitter_handle.csv', 'rb') as ifile:
     reader1 = csv.reader(ifile)
     for row in reader1:
       cursor.execute('INSERT INTO  TWITTER_HANDLE VALUES (?)', row )

ifile.close()

cursor.execute('DELETE FROM TWITTER_HANDLE WHERE twitter_handle = "NA"')
connection.commit()    

#remove string character(i.e. remove u's) check how the table looks like   
#check how the table looks like
cursor.execute('SELECT twitter_handle FROM TWITTER_HANDLE')
connection.text_factory = str
cursor.execute('SELECT twitter_handle FROM TWITTER_HANDLE')   
print cursor.fetchall()

#########################################################
#PART II: Gain and confirm authorization to twitter
##########################################################
import json
import twitter
import sys
sys.path.append("/Users/hyunjincho/Documents/Modules")
print sys.path

#gain authorization to twitter
consumer_key = 'a'
consumer_secret = 'b'
oauth_token = 'c'
oauth_token_secret = 'd'
auth = twitter.oauth.OAuth(consumer_key, consumer_secret, oauth_token, oauth_token_secret)
api=twitter.Twitter(auth=auth)


##########################################################################
#PART III: Twitter manipuation using tables from PART II
##########################################################################
import time

for element in cursor.execute('SELECT twitter_handle FROM TWITTER_HANDLE'):       
    #time.sleep(5) #to get around the rate limit
    element =', '.join( element )     
    element.replace(",","")
    start = api.users.lookup()

这些代码本身在语法方面不会产生任何错误。但是,会出现以下错误:

---------------------------------------------------------------------------
TwitterHTTPError                          Traceback (most recent call last)
<ipython-input-7-e4934ec45f7a> in <module>()
    6     #element.replace(",","")
    7     #print cursor.fetchone()
--> 8     start = api.users.lookup()
    9 

/Users/hyunjincho/anaconda/python.app/Contents/lib/python2.7/site-packages/twitter/api.pyc 
in __call__(self, **kwargs)
    202 
    203         req = urllib_request.Request(uriBase, body, headers)
--> 204         return self._handle_response(req, uri, arg_data, _timeout)
    205 
    206     def _handle_response(self, req, uri, arg_data, _timeout=None):

/Users/hyunjincho/anaconda/python.app/Contents/lib/python2.7/site-packages/twitter/api.pyc
in _handle_response(self, req, uri, arg_data, _timeout)
    233                 return []
    234             else:
--> 235                 raise TwitterHTTPError(e, uri, self.format, arg_data)
    236 
    237 class Twitter(TwitterCall):

TwitterHTTPError: Twitter sent status 401 for URL: 1.1/users/lookup.json using parameters:    
(oauth_consumer_key=35WksHhvf3MnVjJjzciEfl84PJkGNWDwvZZtE72ix4&oauth_nonce=
9964503666055746364&oauth_signature_method=HMAC-       
SHA1&oauth_timestamp=1382631721&oauth_token=xdbX1g21REs0MPxofJPcFw&oauth_version=1.0&
oauth_signature=E%2F722PPfPcn64LqbPMdpo4KiK0o%3D)
details: {"errors":[{"message":"Invalid or expired token","code":89}]}

我认为出现这种错误的原因是因为twitter_handleDB 表列中的每个元素TWITTER_HANDLE最后都有逗号,所以当 twitter 开始查找用户(3Degree例如用户名)时,它会寻找3Degree,而不是3Degree

这种猜想的原因是因为当我执行时:

 for element in cursor.execute('SELECT twitter_handle FROM TWITTER_HANDLE'):       
    #time.sleep(5) #to get around the rate limit
    element =', '.join( element )     
    element.replace(",","")
    print cursor.fetchone()

它给出了类似的东西:

 ('3Degrees',) #<----note the comma at the end
 ('aandrsolar',)

代替

 ('3degrees')  #<---no comma at the end
 ('aandrsolar')

我对么?如果没有,为什么我会不断出现这种错误?谁能帮我?谢谢你...

4

0 回答 0