前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >超级方便的微博用户信息爬虫

超级方便的微博用户信息爬虫

作者头像
月小水长
发布2021-09-24 11:18:18
1.8K0
发布2021-09-24 11:18:18
举报
文章被收录于专栏:月小水长月小水长

今天更新的是微博用户信息爬虫,不是用户爬虫,用户爬虫爬的用户主页发过的微博,用户爬虫用 cn 站的还可以用 一个爬取用户所有微博的爬虫,还能断网续爬那种;而微博用户信息爬虫指的是,根据微博用户 id,抓取用户的阳光信用、性别、地区、学校、公司等信息

代码全部开源在 WeiboSuperSpider 的 github 仓库地址,功能独立版文件夹下,取名 WeiboUserInfoSpider

代码语言:javascript
复制
https://github.com/Python3Spiders/WeiboSuperSpider

或者点击文末的阅读原文直达源代码文件。

拿到代码后,需要填一下 headers 里面的 cookie,随便打开 weibo.com 站点里一个人的主页,比如

代码语言:javascript
复制
https://weibo.com/u/1764201374

也可以是

代码语言:javascript
复制
https://weibo.com/xiena

这种形式,一般比较大咖的的人的纯数字 uid 都被解析成数字+字母形式的 uid 了

然后 F12 开始找 info 或者 detail 这两个 path 之一,复制它们的 cookie 即可。

然后就可以运行这份代码了。

核心代码是根据 uid 获取 userinfo 信息,如下

代码语言:javascript
复制
def getUserInfo(uid):
    try:
        uid = int(uid)
    except:
        # 说明是 xiena 这样的英文串
        uid = parseUid(uid)
        if not uid:
            return None
    response = requests.get(url=f'https://weibo.com/ajax/profile/detail?uid={uid}', headers=headers)
    resp_json = response.json().get('data', None)
    if not resp_json:
        return None
    sunshine_credit = resp_json.get('sunshine_credit', None)
    if sunshine_credit:
        sunshine_credit_level = sunshine_credit.get('level', None)
    else:
        sunshine_credit_level = None
    education = resp_json.get('education', None)
    if education:
        school = education.get('school', None)
    else:
        school = None

    location = resp_json.get('location', None)
    gender = resp_json.get('gender', None)

    birthday = resp_json.get('birthday', None)
    created_at = resp_json.get('created_at', None)
    description = resp_json.get('description', None)
    # 我关注的人中有多少人关注 ta
    followers = resp_json.get('followers', None)
    if followers:
        followers_num = followers.get('total_number', None)
    else:
        followers_num = None
    return {
        'sunshine_credit_level': sunshine_credit_level,
        'school': school,
        'location': location,
        'gender': gender,
        'birthday': birthday,
        'created_at': created_at,
        'description': description,
        'followers_num': followers_num
    }

如果是 uid 是上面所说的第二种形式,不是纯数字的,也会自动解析成数字形式

代码语言:javascript
复制
def parseUid(uid):
    response = requests.get(url=f'https://weibo.com/ajax/profile/info?custom={uid}', headers=headers)
    try:
        return response.json()['data']['user']['id']
    except:
        return None

这样只是单独获取某一个 user 的 info,怎么批量获取呢?比如我们利用 2021 新版微博评论及其子评论爬虫发布 爬取了某一条微博的评论,想要获取这些评论者的所有 userinfo,分析它们的地区分布或者性别比例,下面的代码就是干这个的

代码语言:javascript
复制
def dfAddUserInfo(file_path, user_col, user_info_col='user_info'):
    '''
    @params file_path 指定路径
    @params user_col 指定用户主页链接在那一列, 比如评论csv文件的是 comment_user_link
    @params user_info_col 指定新加的 userinfo 列名,默认是 user_info
    '''
    df = pd.read_csv(file_path)
    user_info_init_value = 'init'
    columns = df.columns.values.tolist()
    if not user_info_col in columns:
        df[user_info_col] = [user_info_init_value for _ in range(df.shape[0])]
    for index, row in df.iterrows():
        print(f'   {index+1}/{df.shape[0]}   ')
        if not row.get(user_info_col, user_info_init_value) is user_info_init_value:
            print('skip')
            continue
        user_link = row[user_col]
        user_id = user_link[user_link.rindex('/')+1:]
        user_info = getUserInfo(user_id)
        print(user_info)
        if user_info:
            # 在 user_info 中统一为 user_link
            user_info['user_link'] = user_link
            df.loc[index, user_info_col] = json.dumps(user_info)
            sleep(1)
        else:
            print(user_link)
            break
    df.to_csv(file_path, index=False, encoding='utf-8-sig')

这个函数会把新加的 user_info 字典以 json 形式加到原来的 csv 中,自动新增一列,列名默认取名 user_info

至于怎么在加了 user_info 的 csv 中遍历想要的地区,性别,学校等信息,代码也有举例,本文的所有源代码可以点击阅读原文直达

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-09-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 月小水长 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 今天更新的是微博用户信息爬虫,不是用户爬虫,用户爬虫爬的用户主页发过的微博,用户爬虫用 cn 站的还可以用 一个爬取用户所有微博的爬虫,还能断网续爬那种;而微博用户信息爬虫指的是,根据微博用户 id,抓取用户的阳光信用、性别、地区、学校、公司等信息。
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档