def get_blog_reposts(cls, weibo_id, raw=False): """获得一条微博的转发""" db = DB.get_crawlerdb() cookies_id, cookies = Cookies.get_cookies(cls.cookies_policies, cls.cookies_type, db) if not cookies: return None params = { 'id': weibo_id } url = 'https://m.weibo.cn/api/statuses/repostTimeline' reposts = [] for page_id in range(1, 2): params['page'] = page_id r = requests.get(url, params=params, cookies=cookies, timeout=3) Cookies.update_cookies(cookies_id, cls.cookies_type, r, db) raw_result = r.json() if raw_result["ok"] is 0: raise RequestException(raw_result['msg']) else: raw_data = raw_result["data"] if raw: reposts += raw_data['data'] else: for l in raw_data['data']: reposts.append(WeiboAPI.parse_raw_blog(l)) return reposts
def get_blog_detail(cls, weibo_id, raw=False): """ 获得一条微博详细信息。 :param raw: 是否返回raw data :param weibo_id: 微博id """ db = DB.get_crawlerdb() cookies_id, cookies = Cookies.get_cookies(cls.cookies_policies, cls.cookies_type, db) if not cookies: return None url = 'https://m.weibo.cn/statuses/show?id={}'.format(weibo_id) r = requests.get(url, cookies=cookies, timeout=3) Cookies.update_cookies(cookies_id, cls.cookies_type, r, db) raw_result = r.json() if raw_result["ok"] is 0: raise RequestException(raw_result['msg']) else: if raw: return raw_result else: raw_blog = raw_result["data"] blog = WeiboAPI.parse_raw_blog(raw_blog) return blog
def get_user_blogs(cls, uid, raw=False): """Ta发的微博""" db = DB.get_crawlerdb() cookies_id, cookies = Cookies.get_cookies(cls.cookies_policies, cls.cookies_type, db) if not cookies: return None params = { 'type': uid, 'value': uid, 'containerid': '107603{}'.format(uid), 'uid': uid } blogs = [] for page_id in range(1, 2): params['page'] = page_id r = requests.get(cls.BASE_URL, params=params, cookies=cookies, timeout=3) Cookies.update_cookies(cookies_id, cls.cookies_type, r, db) raw_result = r.json() if raw_result["ok"] is 0: raise RequestException(raw_result['msg']) else: if raw: blogs.append(raw_result) else: for item in raw_result["data"]["cards"]: if item["card_type"] == 9: raw_blog = item["mblog"] blog = WeiboAPI.parse_raw_blog(raw_blog) blogs.append(blog) return blogs
def get_user_following(cls, uid): """Ta关注的人""" uid = '%d' % float(uid) db = DB.get_crawlerdb() cookies_id, cookies = Cookies.get_cookies(cls.cookies_policies, cls.cookies_type, db) if not cookies: return [] params = { 'luicode': '10000011', 'lfid': '100505{}'.format(uid), 'containerid': '231051_-_followers_-_{}'.format(uid), 'featurecode': '20000320', 'type': 'uid', 'value': uid } followed_id_list = [] for page_id in range(1, 2): params['page'] = page_id r = requests.get(cls.BASE_URL, params=params, cookies=cookies, timeout=3) Cookies.update_cookies(cookies_id, cls.cookies_type, r, db) raw_result = r.json() if raw_result["ok"] is 0: raise RequestException(raw_result['msg']) else: if raw_result['data'].get('cards'): for card in raw_result['data']['cards']: for u in card['card_group']: if u['card_type'] == 10: followed_id_list.append(u["user"]["id"]) return followed_id_list
def get_user_info(cls, uid, raw=False): """获取用户基础信息""" uid = '%d' % float(uid) db = DB.get_crawlerdb() cookies_id, cookies = Cookies.get_cookies(cls.cookies_policies, cls.cookies_type, db) if not cookies: return [] params = { 'containerid': '100505{}'.format(uid), 'type': 'uid', 'value': uid } r = requests.get(cls.BASE_URL, params=params, cookies=cookies, timeout=3) Cookies.update_cookies(cookies_id, cls.cookies_type, r, db) raw_result = r.json() if raw_result["ok"] is 0: raise RequestException(raw_result['msg']) else: if raw: return raw_result else: user_info_raw = raw_result["data"]["userInfo"] user_info = WeiboAPI.parse_user_info(user_info_raw) return user_info
def get_blog_comments(cls, weibo_id, raw=False): """ 获得一条微博下的评论。 分页接口,一次只拿20个。 :param weibo_id: 微博的id :param raw: 是否返回raw data """ db = DB.get_crawlerdb() cookies_id, cookies = Cookies.get_cookies(cls.cookies_policies, cls.cookies_type, db) if not cookies: return None params = { 'id': weibo_id, 'mid': weibo_id } url = 'https://m.weibo.cn/comments/hotflow' comments = [] for _ in range(1, 2): r = requests.get(url, params=params, cookies=cookies, timeout=3) Cookies.update_cookies(cookies_id, cls.cookies_type, r, db) raw_result = r.json() if raw_result["ok"] is 0: raise RequestException(raw_result['msg']) else: raw_data = raw_result["data"] params['max_id'] = raw_data["max_id"] params['max_id_type'] = raw_data['max_id_type'] if raw: comments += raw_data['data'] else: for c in raw_data['data']: comments.append(WeiboAPI.parse_raw_blog(c)) return comments