ZeAy Blog

使用Python解析快手关系JSON

这个是视频版

OK,随便说一下吧 我也是刚刚入门Python 这个就是我写的第一个Python程序,可以说写个博客记录一下

我是没事干开着抓包进入快手平台 就发现这个接口 这个接口就是应该以前小宠物游戏那里抓到的https://activity.m.kuaishou.com/rest/wd/relation/friends,点击进入会发现显示

{
  "result": 109,
  "error_msg": "当前登录失效,请重新登录。",
  "host-name": "public-bjmt-c34-kce-node1380.idchb1az4.hb1.kwaidc.com"
}

原因就是你还没登录 这个链接会记录Cookie 我看Python教程可以请求并且加上Cookie伪装请求 ,这个接口我早些年就发现了 觉得没什么用就不管它了 正好入门Python可以拿零练练手,那么好我就开始写代码了...

老样子新建.py文件 前面就导入requests还有json然后直接请求接口

url = 'https://activity.m.kuaishou.com/rest/wd/relation/friends'
headers = {
    'User-Agent': 'a',
    'Cookie': cookie

User-Agent 这个没什么事,主要是Cookie 如果你觉得不好 可以使用你的设备信息

Cookie 其实不用那么长 输入token就行了

cookie = input("请输入Cookie信息: ")

这个是给用户直接输入Cookie的 然后就是链接状态码了 这个不用写上去是我调试用的嘻嘻,接着就是错误分析了前面不是直接请求了嘛 "error_msg": "当前登录失效,请重新登录。", 直接打印error_msg就行了哈哈 后面就是分析了 我就不多说了 凌晨要睡觉了明天还得去学校~~~ 直接放出来给你们参考

 print("-" * 30) 
    if 'friends' in data:
        for friend in data['friends']:
            print(f"用户ID: {friend['user_id']}")
            print(f"用户名: {friend['user_name']}")
            print(f"快手号: {friend.get('kwaiId', '无')}")
            print(f"性别: {'男' if friend['user_sex'] == 'M' else '女' if friend['user_sex'] == 'F' else '未知'}")
            print(f"是否关注: {'是' if friend['following'] else '否'}")
            print(f"互动详情: {friend['extra']['subTitle']}")
            print(f"头像URL: {friend['headurl']}")
            print("备用头像链接:")
            for headurl in friend['headurls']:
                print(f" - {headurl['url']} (来自 {headurl['cdn']})")
            print(f"访客是否已被关注: {'是' if friend['visitorBeFollowed'] else '否'}")
            print(f"EID: {friend['eid']}")
            print("-" * 30) 

OK,程序就差不多了 下面就是源代码了

from datetime import date
import requests
import json

print("欢迎使用,ZeAy快手关系整理")
print("-" * 30) 
cookie = input("请输入Cookie信息: ")

url = 'https://activity.m.kuaishou.com/rest/wd/relation/friends'
headers = {
    'User-Agent': 'a',
    'Cookie': cookie
}

print("-" * 30) 
response = requests.get(url, headers=headers)
print("链接状态码:", response.status_code)
print("-" * 30) 

try:
    data = response.json()
    if 'error_msg' in data:
        print(f"错误信息: {data['error_msg']}")
    else:
        print("没有错误")
    
    print("-" * 30) 
    if 'friends' in data:
        for friend in data['friends']:
            print(f"用户ID: {friend['user_id']}")
            print(f"用户名: {friend['user_name']}")
            print(f"快手号: {friend.get('kwaiId', '无')}")
            print(f"性别: {'男' if friend['user_sex'] == 'M' else '女' if friend['user_sex'] == 'F' else '未知'}")
            print(f"是否关注: {'是' if friend['following'] else '否'}")
            print(f"互动详情: {friend['extra']['subTitle']}")
            print(f"头像URL: {friend['headurl']}")
            print("备用头像链接:")
            for headurl in friend['headurls']:
                print(f" - {headurl['url']} (来自 {headurl['cdn']})")
            print(f"访客是否已被关注: {'是' if friend['visitorBeFollowed'] else '否'}")
            print(f"EID: {friend['eid']}")
            print("-" * 30) 
    else:
        print("未找到朋友信息")
except json.JSONDecodeError:
    print("错误:响应内容不是有效的JSON格式")
except Exception as e:
    print(f"发生错误: {e}")

input("完毕感谢使用OVO")