虎牙直播源Python爬虫

这段源码可以爬的是虎牙“一起看”的版块即轮播电影那个区域的,其他版块的需要自行修改。

import requests
import re
import json
from lxml import etree
 
class Huya_live:
  
    def __init__(self):
        # self.list_url = []
        self.id = []
        self.headers = {
                "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
            }
  
    def get_m3u8(self):
        for id in self.id:
            url = "https://www.huya.com/" + id
            response = requests.get(url, headers=self.headers)
            html = response.text
            # title = re.findall("<title>.*|\r\n</title>", html)[0]
            html_etree = etree.HTML(html)
            title = html_etree.xpath('//h1[@id="J_roomTitle"]/text()')[0]
            title = re.sub("<title>|</title>|\r|\n", "", title)
            regex = re.compile(r"{.*}")#匹配{}格式的内容
            strs = re.findall(regex, html)
            if id == "523923" or id == "627859" or id == "880219" or id == "11602041" or id == "11352941" or id == "11352969" or id == "880203" or id == "11342439" or id == "17089810":
                    str = strs[1].replace("(|)", "")[:-1]
            else:
                str = strs[2].replace("(|)", "")[:-1]
            str = json.loads(str)
            try:
                streamName = "/" + str['data'][0]['gameStreamInfoList'][1]['sStreamName']
                m3u8 = str['data'][0]['gameStreamInfoList'][1]['sHlsUrl']
                # flv = str['data'][0]['gameStreamInfoList'][1]['sFlvUrl']
                print(title, "\t\t\t\t", m3u8+streamName + ".m3u8")
            except:
                print(url, "获取失败")
                continue
            self.put_write( m3u8 + streamName + ".m3u8", title)
  
    # def get_hrefs(self):
    #     base_url = "https://www.huya.com/g/seeTogether"
    #     response = requests.get(base_url, headers=self.headers).text
    #     html = etree.HTML(response)
    #     hrefs = html.xpath('//li[@class="game-live-item"]/a/@href')
    #     for href in hrefs[::2]:
    #         self.list_url.append(href)
  
    def get_romId(self):
  
        for i in range(1, 5):
            yiqikan_Api = "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2135&tagAll=0&page=%d" % i
            # xingshow_api = "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1663&tagAll=0&page=%d" % i
            response = requests.get(yiqikan_Api, headers=self.headers).text
            html = json.loads(response)
            ids = html['data']['datas']
            for id in ids:
                self.id.append(id['profileRoom'])
 
    def put_write(self, m3u8, title):
        str = title + "***"*20 + m3u8 + "\n"
        with open("./虎牙直播源.txt", 'a+') as f:
            f.write(str)
             
if __name__ == '__main__':
    huya = Huya_live()
    # huya.get_hrefs()
    huya.get_romId()
    huya.get_m3u8()

转自 @niebaohua 仅供参考学习。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

  1. papa papa 说道:

    写的那么好指教一下虎牙和斗鱼怎么抓真实的

  2. 说道:

    请问博主,这个怎么使用啊,有没有教程,指导一下

    1. 黑鸟 说道:

      这个吧只要配置好 Python 的环境,把文章的代码复制为一个.py文件就可以直接运行的 ..

  3. 黑鸟 说道:

    更新版本

    import re
    import requests
    import json
    from lxml import etree
     
     
    class Huya_live:
     
        def __init__(self):
            # self.list_url = []
            self.id = []
            self.headers = {
                "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
            }
     
        def get_m3u8(self):
            for id in self.id:
                url = "https://www.huya.com/" + id
                response = requests.get(url, headers=self.headers)
                html = response.text
                # title = re.findall("<title>.*|\r\n</title>", html)[0]
                html_etree = etree.HTML(html)
                title = html_etree.xpath('//h1[@id="J_roomTitle"]/text()')[0]
                title = re.sub("<title>|</title>|\r|\n", "", title)
                regex = re.compile(r"{\"status\"(.*)};")  # 匹配{}格式的内容
                strs = re.findall(regex, html)
     
                try:
                    a = list(list(strs)[0])
                    a[4] = "{"
                    str = "".join(a[4:])
                    str = json.loads(str)
                    streamName = "/" + str['data'][0]['gameStreamInfoList'][1]['sStreamName']
                    m3u8 = str['data'][0]['gameStreamInfoList'][1]['sHlsUrl']
                    # flv = str['data'][0]['gameStreamInfoList'][1]['sFlvUrl']
                    print(title, "\t\t\t\t", m3u8 + streamName + ".m3u8")
                except:
                    print(url, "直播间没有人直播噢, 不信你去看看 网址为%s" % url)
                    continue
                self.put_write(m3u8 + streamName + ".m3u8", title)
     
        def get_romId(self):
     
            for i in range(1, 5):
                yiqikan_Api = "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2135&tagAll=0&page=%d" % i
                # xingshow_api = "https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1663&tagAll=0&page=%d" % i
                response = requests.get(yiqikan_Api, headers=self.headers).text
                html = json.loads(response)
                ids = html['data']['datas']
                for id in ids:
                    self.id.append(id['profileRoom'])
     
        def put_write(self, m3u8, title):
            str = title + "***" * 20 + m3u8 + "\n"
            with open("./虎牙直播源.txt", 'a+') as f:
                f.write(str)
     
     
    if __name__ == '__main__':
         
        huya = Huya_live()
        huya.get_romId()
        huya.get_m3u8()