from DrissionPage import ChromiumPage, ChromiumOptions from openpyxl import Workbook from openpyxl.styles import Font, Color
if __name__ == '__main__':
input_url = input('请输入目标博主主页链接:\n') user_id = input_url.split("/")[-1] target_url = f'https://weibo.com/u/page/follow/{user_id}?relate=fans'
workbook = Workbook() sheet = workbook.active sheet.append(['昵称', 'ID', '简介', '性别', '粉丝数', '信用分', '创建日期', '城市', '位置', '省份']) for cell in sheet[1]: cell.font = Font(bold=True, color=Color('FFFF0000')) sheet.freeze_panes = 'A2' sheet.column_dimensions['A'].width = 20 sheet.column_dimensions['B'].width = 12 sheet.column_dimensions['C'].width = 30 sheet.column_dimensions['D'].width = 7 sheet.column_dimensions['E'].width = 10 sheet.column_dimensions['F'].width = 10 sheet.column_dimensions['G'].width = 20 sheet.column_dimensions['H'].width = 7 sheet.column_dimensions['I'].width = 10 sheet.column_dimensions['J'].width = 7
co = ChromiumOptions() co.use_system_user_path(True) page = ChromiumPage(co)
page.listen.start('https://weibo.com/ajax/friendships/friends') page.get(target_url)
index = 1 while True: page.scroll.down(500) res = page.listen.wait(timeout=0.5) if res: json_data = res.response.body users = json_data.get('users', []) for user in users: name = user.get('name', '') uid = user.get('id', 0) description = user.get('description', '') gender = user.get('gender', '') followers_count = user.get('followers_count', 0) credit_score = user.get('credit_score', 0) created_at = user.get('created_at', '') city = user.get('city', '') location = user.get('location', '') province = user.get('province', '') info_list = [name, uid, description, gender, followers_count, credit_score, created_at, city, location, province] print(index, info_list) index += 1 sheet.append(info_list)
next_cursor = json_data.get('next_cursor', 0) if next_cursor == 0: break
workbook.save(f'{user_id}.xlsx')
input('\n任务完成,按回车键退出……')
|