当前位置:   article > 正文

scrapy 爬取新浪微博 的微博列表及微博内容_微博查看微博列表的请求

微博查看微博列表的请求

代码地址:GitHub

参考:博客

通过scrapy框架爬取指定账号的信息和微博

截止到目前(2019年01月15日)的微博账号粉丝排名:

爬取方法:提取网页版的微博接口

1.重写start_request方法

  1. def start_requests(self):
  2. weibo_id = [1195354434, ]
  3. for wid in weibo_id:
  4. print('https://m.weibo.cn/api/container/getIndex?type=uid&value=' + str(wid))
  5. yield Request('https://m.weibo.cn/api/container/getIndex?type=uid&value=' + str(wid), callback=self.parse_userInfo, dont_filter=True,
  6. meta={'uid': str(wid)})

2.解析个人信息,并获取containerid

3.爬取博主的微博信息,和他关注的人

  1. # 解析微博列表
  2. def parse_weibo_list(self, response):
  3. # 取相关信息,方便爬取下一页
  4. next_page = str(int(response.meta['page']) + 1)
  5. uid = response.meta['uid']
  6. containerid = response.meta['containerid']
  7. data = response.text
  8. content = json.loads(data).get('data')
  9. cards = content.get('cards')
  10. if (len(cards) > 0):
  11. print("-----正在爬取第%s页-----" % str(response.meta['page']))
  12. for j in range(len(cards)):
  13. card_type = cards[j].get('card_type')
  14. # 微博
  15. # if card_type == 9:
  16. # mblog = cards[j].get('mblog')
  17. # attitudes_count = mblog.get('attitudes_count') # 点赞数
  18. # comments_count = mblog.get('comments_count') # 评论数
  19. # created_at = self.date_format(mblog.get('created_at')) # 发布时间
  20. # reposts_count = mblog.get('reposts_count') # 转发数
  21. # scheme = cards[j].get('scheme') # 微博地址
  22. # # 替换换行后 提取字符串
  23. # text = etree.HTML(str(mblog.get('text')).replace('<br />', '\n')).xpath('string()') # 微博内容
  24. # pictures = mblog.get('pics') # 正文配图,返回list
  25. # pic_urls = [] # 存储图片url地址
  26. # if pictures:
  27. # for picture in pictures:
  28. # pic_url = picture.get('large').get('url')
  29. # pic_urls.append(pic_url)
  30. # uid = response.meta['uid']
  31. # # 保存数据
  32. # sinaitem = SinaItem()
  33. # sinaitem["uid"] = uid
  34. # sinaitem["text"] = text
  35. # sinaitem["scheme"] = scheme
  36. # sinaitem["attitudes_count"] = attitudes_count
  37. # sinaitem["comments_count"] = comments_count
  38. # sinaitem["created_at"] = created_at
  39. # sinaitem["reposts_count"] = reposts_count
  40. # sinaitem["pictures"] = pic_urls
  41. # yield sinaitem
  42. # 关注信息
  43. if card_type == 11:
  44. # 获取他关注的人的地址
  45. # https://m.weibo.cn/p/index?containerid=231051_-_followers_-_1195354434_-_1042015%3AtagCategory_050&luicode=10000011&lfid=1076031195354434 查看该网页的请求过程
  46. fllow_url = str(cards[j]['card_group'][0]['scheme']).replace('https://m.weibo.cn/p/index?', 'https://m.weibo.cn/api/container/getIndex?')
  47. print(fllow_url, '----')
  48. yield Request(url=fllow_url, callback=self.parse_fllow)
  49. # 下一页链接
  50. # weibo_list_url = 'https://m.weibo.cn/api/container/getIndex?type=uid&value=' + uid + '&containerid=' + containerid + '&page=' + next_page
  51. # response.meta['page'] = next_page
  52. # yield Request(weibo_list_url, callback=self.parse_weibo_list, meta=response.meta)

4.根据他关注的人的ID,再次重复此过程

  1. # 获取关注者的信息
  2. def parse_fllow(self, response):
  3. data = response.text
  4. content = json.loads(data).get('data')
  5. cards = content.get('cards')
  6. # if len(cards) > 0:
  7. for card in cards:
  8. if card.get('title') == '他的全部关注':
  9. for tmp in card.get('card_group'):
  10. user = tmp.get('user')
  11. # 获取关注的人的ID
  12. uid = user.get('id')
  13. yield Request('https://m.weibo.cn/api/container/getIndex?type=uid&value=' + str(uid), callback=self.parse_userInfo, dont_filter=True,
  14. meta={'uid': str(uid)})

由于此过程是个循环,需要采取一定的控制条件才能爬取完成(如果不被封IP的话)

可先筛选出你感兴趣的用户,再爬取他的微博

防封的话建议采取代理IP的方式,在下载中间件中添加即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/57069
推荐阅读
相关标签
  

闽ICP备14008679号