微博API:获取用户发布的微博

    技术2022-05-19  26

        最近是由于老师的要求,所有小小地研究下了微博开发平台。

        其实在微博开发平台上做开发很简单,只要上去创建一个应用,拿到source key跟secret就可以开始做自己的应用了。微博的API接口是基本都是通过url提供的,格式有XML跟json两种。获取数据之后你就可以做自己的处理了。

       这里我谈下如何获取指定用户发布的微博,比较郁闷的是,微博API文档上面并没有获取指定用户发布微博这个描述的东西,我找了三遍都没有找到。只找到一个比较接近一点的:获取当前登录用户发布的微博消息列表。但是我要的是指定用户ID的,最后没想到这个API就是我想要的,囧。。。。

        废话不多说了,来说说怎么去获取指定用户发布的微博。

       使用的接口是statuses/user_timeline。

      通过user_id可以指定用户ID,即获取指定用户的微博;通过count可以指定返回微博的条数,最大是200条。微博开发平台也限制了最多给你返回一个用户的最新的200微博。

      下面是一个返回XML数据的样例,可以参考下。

    <?xml version="1.0" encoding="UTF-8"?> <statuses> <status> <created_at>Mon Nov 29 16:08:43 +0800 2010</created_at> <id>3958728723</id> <text>各位开发者,我们的论坛上线啦~http://sinaurl.cn/h4FWc7 欢迎大家的参与~另外,关于技术相关的问题,可以在论坛上提出,也可以@微博API 这个官方技术支持账号哦~感谢大家对开放平台的支持~[呵呵]</text> <source> <a href="http://t.sina.com.cn" mce_href="http://t.sina.com.cn">新浪微博</a> </source> <favorited>false</favorited> <truncated>false</truncated> <geo/> <in_reply_to_status_id/> <in_reply_to_user_id/> <in_reply_to_screen_name/> <user> <id>11051</id> <screen_name>微博开放平台</screen_name> <name>微博开放平台</name> <province>11</province> <city>8</city> <location>北京 海淀区</location> <description>新浪微博开放平台市场推广官方账号,如有技术问题,请@微博API或者发私信给微博API</description> <url>http://open.t.sina.com.cn/</url> <profile_image_url>http://tp4.sinaimg.cn/11051/50/1280283165/1</profile_image_url> <domain>openapi</domain> <gender>m</gender> <followers_count>13034</followers_count> <friends_count>5</friends_count> <statuses_count>157</statuses_count> <favourites_count>0</favourites_count> <created_at>Wed Jan 20 00:00:00 +0800 2010</created_at> <following>false</following> <verified>true</verified> <allow_all_act_msg>true</allow_all_act_msg> <geo_enabled>true</geo_enabled> </user> </status> ... </statuses>

     

        我自己用了python写了一个,把代码贴上来吧,有需要的人可以拿去试试,不过记得给source赋值,把你自己的source key写上去。#!/usr/bin/python # This file is to get the weibo that a user issues # the input is the user id, and the scipt will get all the data import urllib2 # This function is to get the data of the given user in weibo # notice: the system only provide the lateset 200 weibo of one user def getUserData(user_id): # the source key of the develop, you should add your source key here source = # the url to get the data url = "http://api.t.sina.com.cn/statuses/user_timeline.xml?source=%d&user_id=%d&count=200"%(source, user_id) # print url str = urllib2.urlopen(url) return str.read() # the user id that you want to visit uid = 1786642865 str = getUserData(uid) print str exit(0)

     

     


    最新回复(0)