python实现twitter client

公司的代理可以直接穿墙,自由访问Twitter、Facebook等网站,这两天研究了一下Twitter提供的API,用python写了一个twitter client,只实现了基本功能,查看自己的twitter消息,也可以不验证,查看public的twitter消息。其他功能实现类似。主要函数如下:

 

def fetch_with_proxy(proxy, username, password, url):
    proxy_handler = urllib2.ProxyHandler({"http" : proxy})
    opener = urllib2.build_opener(proxy_handler)
    urllib2.install_opener(opener)
    authheader = AddAuthorizationHeader(username, password)
    req = urllib2.Request(url)
    try:
        res = urllib2.urlopen(req)
    except IOError, e:
        if hasattr(e, 'code'):
            if e.code != 401:
                print e.code
            else:
                req.add_header("Authorization", authheader)
                res = urllib2.urlopen(req)
                data= simplejson.loads(res.read())

 

def AddAuthorizationHeader(username, password):
    if username and password:
        basic_auth = base64.encodestring('%s:%s' % (username, password))[:-1]
        return 'Basic %s' % basic_auth

def main(user):
    username='XXX@gmail.com'   #change to your username and password
    password='XXX'
    proxy='host:port'                    #change to proxy host and port
    for page in range(10):
        url = 'http://twitter.com/statuses/friends_timeline/%s.json?page=%s' % (user, page)
        fetch_with_proxy(proxy, username, password, url)

 

if __name__ == '__main__':
    main('')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值