まず第一に、以下は私のコードです:
#################################################################
#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_handle
、DB テーブルの列のすべての要素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')
私は正しいですか?そうでない場合、なぜこの種のエラーが発生し続けるのですか? 誰でも私を助けることができますか?ありがとうございました...