一只快手小爬虫

要爬取的链接:

(点我试试)

 

要爬取的页面:

 

要爬取的内容:

 

先研究下,

 

 

 

如图,每一个用户信息在一个li标签里面,靠css选择器就能很容易获取到,但是看源码却发现那些关键的数字有字体反爬。如下图:

 

然后看到这篇(点我试试)博客后,完成了这只小爬虫~

代码:

import requests
from pyquery import PyQuery as pq
from urllib.parse import urljoin
import json

def parsingChar(type,data):
    fontscn_h57yip2q = {
        '\\uabcf':'4',
        '\\uaced':'3',
        '\\uaedd':'8',
        '\\uaede':'0',
        '\\uafcd':'6',
        '\\ubdaa':'5',
        '\\ubdcd':'1',
        '\\ubfad':'9',
        '\\uccda':'2',
        '\\ucfbe':'7',
    }
    fontscn_3jqwe90k = {
        '\\uaacb':'4',
        '\\uabcd':'3',
        '\\uacdd':'0',
        '\\uaefb':'8',
        '\\uafbc':'6',
        '\\ubbca':'1',
        '\\ubdca':'5',
        '\\ubfee':'9',
        '\\uccac':'2',
        '\\ucfba':'7',
    }
    fontscn_yuh4hy4p = {
        '\\uaabd':'5',
        '\\uaadd':'0',
        '\\uacde':'9',
        '\\uadaa':'2',
        '\\uadac':'1',
        '\\uadcb':'7',
        '\\uaeed':'8',
        '\\ubebb':'3',
        '\\ucbdc':'6',
        '\\ucccf':'4',
    }
    fontscn_qw2f1m1o = {
        '\\uabcb':'4',
        '\\uaccd':'3',
        '\\uacda':'0',
        '\\uaeff':'8',
        '\\uafbb':'6',
        '\\ubdca':'1',
        '\\ubdcc':'5',
        '\\ubfef':'9',
        '\\uccaa':'2',
        '\\ucfba':'7',
    }
    fontscn_yx77i032 = {
        '\\uabce':'4',
        '\\uaccd':'6',
        '\\uaeda':'8',
        '\\uaefe':'0',
        '\\uafed':'3',
        '\\ubaaa':'5',
        '\\ubddd':'1',
        '\\ubfad':'2',
        '\\ubfae':'9',
        '\\uc44f':'7',
    }
    woff_dict = {'h57yip2q': fontscn_h57yip2q, '3jqwe90k': fontscn_3jqwe90k, 'yuh4hy4p': fontscn_yuh4hy4p,
                 'qw2f1m1o': fontscn_qw2f1m1o, 'yx77i032': fontscn_yx77i032}
    li = []
    new_data = (list(map(lambda x: x.encode('unicode_escape'), data)))
    #这里将data转为编码byte型的数据,如b'\\ubdca'
    for i in new_data:
        if len(str(i)) > 5:
            num = woff_dict[type][str(i)[3:-1]]
            #str(i)[3:-1]这里是将比如b'\\ubdca'转为字符串\ubdca,好去字典中匹配值
            li.append(num)
        else:
            li.append(str(i)[2:-1])
    res = ''.join(li)
    return res


def handling_detail(word,type):
    '''
    :param word: 含细节的字符串
    :param type: 当前页面字体类型
    :return: 将数字转换成正常的后返回
    '''
    try:
        words = word.split('  ')
        if 'w粉丝' in words[0]:
            fans = words[0].replace('w粉丝', '').strip()
            fans = parsingChar(type, fans)+'w粉丝'
        else:
            fans = words[0].replace('粉丝', '').strip()
            fans = parsingChar(type, fans)+'粉丝'
        #转换粉丝数为正常数字
        follows = words[1].strip().replace('关注','')
        follows = parsingChar(type,follows)
        # 转换关注数为正常数字
        works = words[2].strip().replace('作品', '')
        works = parsingChar(type,works)
        # 转换作品数为正常数字
        all = fans+follows+'关注'+works+'作品'
        return all
    except:
        print(word,'handling_detail error')


def judge(html):
    '''
    :param html: html源码
    :return: 当前页面字体类型
    '''
    for i in ['h57yip2q', '3jqwe90k','yuh4hy4p', 'qw2f1m1o', 'yx77i032']:
        if i in html:
            return i


def getList(key,page):
    '''
    :param key: 搜索的关键字
    :param page: 页数
    :return: 用户的一些细节,用户名,用户主页url,用户画像,用户签名,用户粉丝数等等...
    '''
    all = {}
    url = 'https://live.kuaishou.com/search/author?keyword='+key+'&page='+str(page)
    original_url = 'https://live.kuaishou.com'
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    r = requests.get(url=url,headers=headers)
    type = judge(r.text)#判断当前页面字体类型
    html = pq(r.text)
    lis = html('.search-detail ul .author-card').items()

    for li in lis:
        '''
        这里是用pyquery的css选择器对用户信息的获取
        '''
        name = li('.profile-card-user-info-intro').attr('title').strip()
        #用户名
        detail = li('.profile-card-user-info-counts').text().strip()
        detail = handling_detail(detail,type)#对有字体反爬地方处理
        #粉丝数作品数有反爬的部分
        sign = li('.profile-card-user-info-description.notlive').text().strip()
        #签名
        user_url = li('.profile-card-user-info a').attr('href').strip()
        user_url = urljoin(original_url,user_url)
        #主播首页url
        user_img = li('img').attr('src').strip()
        #用户画像url
        all[name] = {'user_url':user_url,'detail':detail,'sign':sign,'user_img':user_img}
    return all


if __name__ == '__main__':
    key = '技能'
    for i in range(1,11):
        with open('kuaishou.json','a',encoding='utf-8') as f:
            json.dump(getList(key,i), f, ensure_ascii=False, sort_keys=True, indent=4)
            #json文件保存获取的内容

 结果:

 

转载于:https://www.cnblogs.com/byadmin/articles/spider-2.html

### 快手直播爬虫实现方法 #### 1. 理解快手直播爬虫的核心需求 快手直播是一种实时流媒体服务,其据传输通常基于特定协议(如HLS或RTMP),并可能伴随动态加密机制。因此,要实现快手直播的爬虫,需了解以下几个方面: - **请求解析**:分析快手直播页面中的API接口调用逻辑,提取播放地址的相关参[^2]。 - **反混淆处理**:部分直播链接经过加密或混淆处理,需要逆向工程找到解密算法[^3]。 #### 2. 技术栈选择 尽管大多爬虫教程推荐使用Python作为开发语言,但对于涉及多媒体流的场景,还需要引入额外库支持: - `requests` 或 `httpx`:用于发送HTTP/HTTPS请求获取初始据。 - `re` 和 `json`:帮助解析返回的内容结构。 - `ffmpeg` 或其他音视频工具:如果目标是对直播流进行录制,则需要这些工具完成实际抓取工作。 下面给出一段简单的伪代码框架展示如何初步尝试访问某个直播间的信息: ```python import requests from bs4 import BeautifulSoup as soup def fetch_live_info(room_id): url = f"https://live.kuaishou.com/u/{room_id}" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" } response = requests.get(url, headers=headers) page_content = response.text parsed_data = soup(page_content, 'html.parser') script_tags = parsed_data.find_all('script') for tag in script_tags: content = str(tag.string).strip() if '"playUrl"' in content: json_obj = eval(content.replace('false', 'False').replace('true', 'True')) play_url = json_obj['playUrl'] return play_url if __name__ == "__main__": room_identifier = input("请输入房间ID:") live_stream_address = fetch_live_info(room_identifier) print(f"直播源地址:{live_stream_address}") ``` 注意以上仅为演示用途的实际应用中还需考虑更多细节比如异常捕获、代理配置以及遵守平台的服务条款等问题[^1]。 #### 3. 面临挑战与注意事项 - 平台防护措施日益增强,单纯依赖公开可得资料难以长期稳定运行此类脚本; - 尊重版权法律及各网站robots.txt规定,在未经授权情况下大规模采集他人作品存在风险; --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值