免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4242 | 回复: 3
打印 上一主题 下一主题

有个新浪微薄爬虫的问题 KeyError [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-01-09 09:28 |只看该作者 |倒序浏览
  1. #! /usr/bin/env python
  2. # -*- coding = utf-8 -*-

  3. from time import sleep

  4. class WeiboSpider:
  5.     '''
  6.         crawl sina weibo comments
  7.     '''
  8.     def __init__(self, user_id):
  9.         self.user_id = user_id

  10.     def delay_time(self):
  11.         '''
  12.             limit to access sina server delay time
  13.         '''
  14.         sleep(60)

  15.     def get_friendships(self, user_id):
  16.         '''
  17.             get user's(uid=user_id) following list
  18.             default setting is 500
  19.         '''
  20.         self.user_id = user_id

  21.         friendship = client.get.friendships__friends__ids(uid=user_id)

  22.         uid_set = []

  23.         for each_uid in friendship.ids:
  24.             uid_set.append(each_uid)

  25.         return uid_set

  26.     def get_user_info(self, user_id):
  27.         '''
  28.             get user id=user_id basic information
  29.         '''
  30.         self.user_id = user_id

  31.         user_info = client.get.user__show(uid=user_id)

  32.         user = []

  33.         user.append(user_info.id)
  34.         user.append(user_info.screen_name)
  35.         user.append(user_info.gender)

  36.         return user

  37.     def get_statuses(self, user_id):
  38.         '''
  39.             get 50 statuses from user id=user_id
  40.         '''
  41.         self.user_id = user_id

  42.         status = client.get.statuses__user_timeline(uid=user_id, count=50)

  43.         status_id       = []
  44.         status_writer   = []
  45.         status_time     = []
  46.         status_text     = []
  47.         status_comments = []
  48.         
  49.         for each_status in status.statuses:
  50.             status_id.append(each_status.id)
  51.             status_writer.append(each_status.user.screen_name)
  52.             status_time.append(each_status.created_at)
  53.             status_text.append(each_status.text)
  54.             status_comments.append(each_status.comments_count)

  55.         return (status_id, status_writer, status_time, status_text, status_comments)

  56.     def get_comments(self, status_id):
  57.         '''
  58.             get 50 comments on statuses id=status_id
  59.         '''
  60.         self.status_id = status_id

  61.         comment = client.get.comments__show(id=status_id, count=50)

  62.         comment_writer_id = []
  63.         comment_writer    = []
  64.         comment_time      = []
  65.         comment_text      = []

  66.         for each_comment in comment.comments:
  67.             comment_writer_id.append(each_comment.user.id)
  68.             comment_writer.append(each_comment.user.screen_name)
  69.             comment_time.append(each_comment.created_at)
  70.             comment_text.append(each_comment.text)

  71.         return (comment_writer_id, comment_writer, comment_time, comment_text)

  72.             
  73. if __name__ == "__main__":
  74.     from client import Client

  75.     client = Client()
  76.     client = client.set_client()

  77.     user_id = raw_input("Please input user's id: ")
  78.     spider = WeiboSpider(user_id)

  79.     uid_set = spider.get_friendships(user_id)

  80.     out_file = open('comments.dat', 'w')

  81.     for each_uid in uid_set:
  82.         (temp_status_id, temp_status_writer, temp_status_time,
  83.          temp_status_text, temp_status_comments) = spider.get_statuses(each_uid)

  84.         print "number of statuses before filter: %d" % len(temp_status_id)

  85.         status_id       = []
  86.         status_writer   = []
  87.         status_time     = []
  88.         status_text     = []
  89.         status_comments = []

  90.         for k in range(len(temp_status_id)):
  91.             if temp_status_comments[k] != 0:
  92.                 status_id.append(temp_status_id[k])
  93.                 status_writer.append(temp_status_writer[k])
  94.                 status_time.append(temp_status_time[k])
  95.                 status_text.append(temp_status_text[k])
  96.                 status_comments.append(temp_status_comments[k])

  97.         print "number of statuses after filter: %d" % len(status_id)
  98.         
  99.         for i in range(len(status_id)):
  100.             print ">Grabbing data of: %s" % status_writer[i]
  101.             writer = status_writer[i].encode('utf-8')

  102.             spider.delay_time()

  103.             (comment_writer_id, comment_writer, comment_time,
  104.              comment_text) = spider.get_comments(status_id[i])

  105.             for j in range(len(comment_text)):
  106.                 if comment_writer_id[j] == int(user_id):
  107.                     name = comment_writer[j].encode('utf-8')
  108.                     time = comment_time[j].encode('utf-8')
  109.                     text = comment_text[j].encode('utf-8')
  110.    
  111.                     out_file.write(name + ' ' + writer + ' ' +
  112.                                    time + ' ' + text + '\n')

  113.     out_file.close()
  114.    



复制代码
这是个 新浪微薄的爬虫程序 抓取某个用户在其所关注用户上的微薄的评论( 突发奇想就写了个。。。。)

由于新浪的限制(我用了官方的API  太复杂的爬虫不会写)

访问时间间隔设置了60秒 所以要爬很长时间

可是运行几个小时后就弹出  KeyError: 'user'


这样的
  1. Traceback (most recent call last):
  2.   File "spider.py", line 112, in <module>
  3.     temp_status_text, temp_status_comments) = spider.get_statuses(each_uid)
  4.   File "spider.py", line 68, in get_statuses
  5.     status_writer.append(each_status.user.screen_name)
  6.   File "weibo.py", line 50, in __getattr__
  7.     return self[attr]
复制代码
weibo.py是官方的SDK
spider.py是我上面的代码

没查出原因  新浪server的原因么?
导致 user这个变量没值?

论坛徽章:
0
2 [报告]
发表于 2013-01-09 09:44 |只看该作者
本帖最后由 你还未够水准呢 于 2013-01-09 09:45 编辑

初步学python 代码不足之处 还望 @linux_c_py_php 指正

论坛徽章:
0
3 [报告]
发表于 2013-01-09 10:35 |只看该作者
官方api也不是万能的,新浪微博的python api,貌似是第三方维护的,不是新浪官方,新浪的官方语言是php

所以,feel free去weibo.py里面排错吧,python的调试信息说的蛮清楚了,连行数都标出来了,自己动手丰衣足食

论坛徽章:
0
4 [报告]
发表于 2013-01-10 14:28 |只看该作者
bug 找到了 有的微薄被删掉了 但是依然从服务器返回 但是返回了空的列表 所以就append出错

个人猜测。。。。。添加了判断语句
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP