Java抓取新浪微博及评论

申请新浪微博授权

1.下载新浪微博的SDK,并解压,导入到Eclipes中,下载地址http://download.csdn.net/download/itjavaer/8331717,或到新浪网站下载

2.打开http://open.weibo.com/网站用自己的微博帐号登陆

3.点击网页右上角的头像,选择编辑开发者信息。开发者类型选择公司,信息可随便填写,网站不会审核。

4. 上网页上方导航栏处点击微连接,点击创建应用,选择网页应用

5. 接下来填写,应用名称,应用地址,应用分类,随便写就可以了。

6.在导航栏进入我的应用,点击刚刚创建的应用。进入该应用的详细页面

7.在左侧点击应用信息,进入基本信息界面将App Key和AppSecret记录下来,这两个值要配置到项目中。

8.进入高级信息,设置编辑OAuth2.0 授权,授权回调页和取消授权回调页写www.baidu.com即可,或者写你自己或公司的地址,这个地址一定要在浏览器能访问,否者不能申请授权。

9.到Eclipse的weibo4j-oauth2工程中找到config.properties文件填写下列信息

client_ID=184XXXXXX   (基本信息界面的AppKey)
client_SERCRET=b3397a699a5xxxxxxxxxxxxxxxxxxxxx(基本信息界面的App Secret)

redirect_URI=www.baidu.com(高级信息中的授权回掉页)

其他不需要改

10.在weibo4j-oauth2工程中找到OAuth4Code类,位于weibo4j.examples.oauth2包下,执行该类,执行中会在浏览器打开一个网页,点击授权,这时候会跳转到授权回掉页,这时候会看见百度主页,因为授权回掉页配置的就是百度。然后复制浏览器地址栏的code值,回到eclipse,将code值粘贴到控制台

回车。这时候控制台会打印一些信息,找到最后一行accessToken=2.00vtRLjF0Utxxxxxxxxxxxxxxxxxx,把这个值保存好,上面所有的操作都是为了得到这个值。

抓取新浪微博数据

 

 /**

               * 获取关注人发的微博

               *一次只能返回20条

              *返回的是Status对象,包含作者信息,微博内容,微博MID,回复人,图片地址等信息

               * access_token就是授权码:2.00vtRLjF0Utxxxxxxxxxxxxxxxxxx

               */

         publicstatic List<Status> getListWeibo(String access_token){

                   Timelinetimeline = new Timeline(access_token);

                   StatusWapperstatusWapper = null;

                   try{

                            statusWapper= timeline.getFriendsTimeline();

                   }catch (WeiboException e) {

                            e.printStackTrace();

                   }

                   returnstatusWapper.getStatuses();

         }

 

 

 

新浪apigetFriendsTimeline()方法只能返回20条,要想返回更多只能自己改下新浪api了在Timeline类下重载getFriendsTimeline方法

   

    publicStatusWapper getFriendsTimeline(int count) throws WeiboException {

                   returnStatus.constructWapperStatus(client.get(WeiboConfig

                                     .getValue("baseURL")

                                     +"statuses/friends_timeline.json",

                                     newPostParameter[] { new PostParameter("count", count), },

                                     access_token));

 

         }


新浪应该会提供返回更多微博的api,但我没有找到,只能这样写一个了,要注意新浪有规定,开发账户的权限每小时只能获取100条。

/**

      * 获取评论

      * @param weiboId

      * @param token

      *评论可能会有很多条所以放在了StringBuffer里

      *weiboId就是 status.getId()

      * @return

      */

    public static String getCommentByWeiboId(String weiboId,Stringaccess_token) {

       StringBuffer sbComment = new StringBuffer();

       CommentWapper commentWapper = null;

       Comments comments = new Comments(access_token);

       try {

           commentWapper = comments.getCommentById(weiboId);

           List<Comment> commentList =commentWapper.getComments();

           for(Comment comment : commentList){

              String commentStr=comment.getUser().getScreenName()+":"+comment.getText()+"("+sdf.format(comment.getCreatedAt())+")";

              sbComment.append(commentStr);

              sbComment.append("<br>");

              sbComment.append("\n");

           }

       } catch (WeiboException e) {

           e.printStackTrace();

       }

       return sbComment.toString();

    }


 

 

    

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值