获取微博某博主的粉丝列表

功能介绍:

输入微博某博主的主页链接,获取他的粉丝列表信息。

下载地址:

https://www.123pan.com/s/QZGSVv-cXyN3.html

注意事项:

  1. 电脑安装Chrome浏览器。
  2. 用Chrome浏览器登录微博,然后关闭浏览器。
  3. 打开脚本,输入博主主页链接,类似 https://weibo.com/1767819164
  4. 任务完成后,在脚本同级目录会生成一个以目标博主ID命名的表格文件,即粉丝列表。

代码

pip install DrissionPage

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任务完成,按回车键退出……')