分享

数据抓取:新浪微博数据解析

howtodown 2014-12-10 17:56:57 发表于 实操演练 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 3 19911


导读:
如何解析新浪微博的JSON,对于数据抓取的同学可能比较感兴趣:
思路如下:
1.首先获取json
2.处理json,那么如何处理的的那,可以参考下面内容?







其实一开始的时候,也遇过一些挫折,比如直接用JsonArray和JsonObject去解析JSON内容的话,是解析不了的。
因为JSON的格式比较固定,像新浪微博返回的JSON内容则是多了一个中括号及statues标签,如下:

{
"statuses": [ {
"created_at": "Tue May 31 17:46:55 +0800 2011",
"id": 11488058246,
"text": "求关注。",
"source": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>",
"favorited": false,
"truncated": false,
"in_reply_to_status_id": "",
"in_reply_to_user_id": "",
"in_reply_to_screen_name": "",
"geo": null,
"mid": "5612814510546515491",
"reposts_count": 8,
"comments_count": 9,
"annotations": [],
"user": {
"id": 1404376560,
"screen_name": "zaku",
"name": "zaku",
"province": "11",
"city": "5",
"location": "北京 朝阳区",
"description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。",
"url": "http://blog.sina.com.cn/zaku",
"profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1",
"domain": "zaku",
"gender": "m",
"followers_count": 1204,
"friends_count": 447,
"statuses_count": 2908,
"favourites_count": 0,
"created_at": "Fri Aug 28 00:00:00 +0800 2009",
"following": false,
"allow_all_act_msg": false,
"remark": "",
"geo_enabled": true,
"verified": false,
"allow_all_comment": true,
"avatar_large": "http://tp1.sinaimg.cn/1404376560/180/0/1",
"verified_reason": "",
"follow_me": false,
"online_status": 0,
"bi_followers_count": 215
}
},
... ],
"previous_cursor": 0, // 暂未支持
"next_cursor": 11488013766, // 暂未支持
"total_number": 81655
}

我的目标就是先去掉以上红色字体的内容,这个比较容易实现。
  1. //将获取到的json内容变成可以正常解析的文本  
  2.     public static String parseJsonText(String jtext){  
  3.          
  4.         char leftSymbol=‘[';  
  5.         char rightSymbol=']‘;  
  6.         String result=“”;  
  7.         for(int i=0;i<jtext.length();i++){  
  8.               
  9.             if(leftSymbol==jtext.charAt(i)){  
  10.                 result=jtext.substring(i,jtext.length());  
  11.                 break;  
  12.             }  
  13.         }  
  14.          
  15.         for(int i=result.length();i>0;i–){  
  16.               
  17.             if(rightSymbol==result.charAt(i-1)){  
  18.                 result=result.substring(0, i);  
  19.                 break;  
  20.             }  
  21.         }  
  22.         return result;  
  23.          
  24.     }  
复制代码










处理完就可以直接用JsonArray和JsonObject来解析相关的内容了。
第二步就是实现一个List<HashMap<String,String>>,用于填充ListView。
下面的方法返回一个List<HashMap<String,String>>,并在其中解析JSON内容。
  1. private List<HashMap<String, String>> listview(String rlt) {  
  2.   
  3.         String result = ParseJson.parseJsonText(rlt);  
  4.         try {  
  5.             JSONArray jsonArrays = new JSONArray(result);  
  6.             for (int i = 0; i < jsonArrays.length(); i++) {  
  7.                 JSONObject jsonObject = jsonArrays.getJSONObject(i);  
  8.   
  9.                 HashMap<String, String> map = new HashMap<String, String>();  
  10.                 // 获得微博的ID  
  11.                 String ID = jsonObject.getString(“id”);  
  12.                 contentId.add(ID);  
  13.                 // 获得微博内容  
  14.                 String text = jsonObject.getString(“text”);  
  15.                 // 获得微博来源  
  16.                 String source = jsonObject.getString(“source”);  
  17.                 // 获得微博的发表时间  
  18.                 String created_at = jsonObject.getString(“created_at”);  
  19.                 // 生成user的可用JSON对象  
  20.                 JSONObject userJson = jsonObject.getJSONObject(“user”);  
  21.                 // 获得发表微博的用户名  
  22.                 String username = userJson.getString(“name”);  
  23.   
  24.                 map.put(“text”, text);  
  25.                 map.put(“name”, username);  
  26.                 map.put(“source”, “来自” + “【” + source + “】”);  
  27.                 map.put(“created_at”, ParseJson.getFormatTime(created_at));  
  28.   
  29.                 if (jsonObject.has(“retweeted_status”)) {  
  30.                     JSONObject retweetedJson = jsonObject  
  31.                             .getJSONObject(“retweeted_status”);  
  32.                     // 获得转发微博的内容  
  33.                     String retweetedText = retweetedJson.getString(“text”);  
  34.                     map.put(“retweetedText”, retweetedText);  
  35.                     JSONObject retWeetedUserNameJson = retweetedJson  
  36.                             .getJSONObject(“user”);  
  37.                     // 获得转发微博内容的用户名  
  38.                     String retweetedContent = retWeetedUserNameJson  
  39.                             .getString(“name”);  
  40.                     map.put(“retweetedContent”, retweetedContent + “:”  
  41.                             + retweetedText);  
  42.                 }  
  43.                 listArrays.add(map);  
  44.             }  
  45.   
  46.         } catch (JSONException e) {  
  47.             // TODO Auto-generated catch block  
  48.             System.out.println(e.getMessage());  
  49.         }  
  50.   
  51.         return listArrays;  
  52.   
  53.     }  
复制代码




通过上面两步就可以实现基本的微博JSON解析了。

已有(3)人评论

跳转到指定楼层
EASONLIU 发表于 2014-12-17 10:13:08
路过,学习学习
回复

使用道具 举报

kelongxhu 发表于 2015-1-22 00:08:58
路过。路过。。路过。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条