我用 Python 编写了一个 Twitter 应用程序。以下是我用于模块的代码,我在其中找到 x 是否跟随 y。这段代码可以明显改进。一种pythonic的方式来做到这一点?
import urllib2
import sys
import re
import base64
from urlparse import urlparse
import simplejson
def is_follows(follower, following):
theurl = 'http://twitter.com/friendships/exists.json?user_a='+follower+'&user_b='+following
username = 'uname1'
password = 'pwd1'
handle = urllib2.Request(theurl)
base64string = base64.encodestring('%s:%s' % (username, password))
authheader = "Basic %s" % base64string
handle.add_header("Authorization", authheader)
fol=True
try:
fol = simplejson.load(urllib2.urlopen(handle))
except IOError, e:
# here we shouldn't fail if the username/password is right
print "It looks like the username or password is wrong."
return fol
更新:缩进已修复。