爬取虎牙视频代码如下
import requests
import re
import os
from lxml import etree
import json
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
s=input("请输入要爬取的页数\n")
url='https://v.huya.com/g/all?set_id=49&order=hot&page='+s
headers__text = requests.get(url=url, headers=headers).text
html=requests.get(url=url,headers=headers).text
d=re.findall('',html)
for n in d:url=f'https://liveapi.huya.com/moment/getMomentContent?callback=&videoId={n}&uid=&_=1690430766941'requests_get = requests.get(url=url, headers=headers)title_ = requests_get.json()['data']['moment']['title']
# xp=etree.HTML(headers__text).xpath('//section[@class="mod-wrap"]/ul[2]/li')
# for s in xp:
# xpa = s.xpath('./a/@href')[0]
# Name='https:'+xpa
# print(Name)#url='https://liveapi.huya.com/moment/getMomentContent?callback=jQuery112409191653515556095_1694227988113&videoId=905480831&uid=&_=1694227988119'
#https://v.huya.com/jsapi/isVideoCollected?callback=&vids=#https://liveapi.huya.com/moment/getMomentContent?callback=&videoId={n}&uid=&_=1690430766941'
#https://liveapi.huya.com/moment/getCommentList?callback=jQuery1124041941671267272196_1694784083548&uid=&parentId=7271921012600010177&momId=7271921012600010177&lastComId=0&isGetHotComment=1&_=1694784083553
# r=requests.get(url=url,headers=headers).text
# ex='jQuery112409191653515556095_1694227988113\((.*)'#jQuery1124041941671267272196_1694784083548
#https://liveapi.huya.com/moment/getMomentContent?callback=&videoId=908481994&uid=&_=1690430766941
#https://liveapi.huya.com/moment/getMomentContent?callback=&videoId=908481994&uid=&_=1690430766941# window.HNF_GLOBAL_INIT = {"recommendChannelList":[{"title":"王者荣耀","cover":"http://v-huya-pic.huya.com/vhuya/8f3988d4-a89e-4bc5-a5c6-6f8cb4075afb.png","vid":0,"setId":15,"channelItemList":{"_bValue":0,"value":[{"id":"vhuyawzry","name":"王者荣耀","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":2,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"英雄联盟","cover":"http://v-huya-pic.huya.com/vhuya/b91d898a-d588-4312-9521-1ecdac6bad58.png","vid":0,"setId":13,"channelItemList":{"_bValue":0,"value":[{"id":"vhuyalol","name":"英雄联盟","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":2,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"和平精英","cover":"http://v-huya-pic.huya.com/vhuya/05b9d53d-f594-4098-a303-0ae853c25450.png","vid":0,"setId":9,"channelItemList":{"_bValue":0,"value":[{"id":"cjzc","name":"和平精英","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":2,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"绝地求生","cover":"http://v-huya-pic.huya.com/vhuya/5222b536-9537-45f5-8843-6b2223204849.png","vid":0,"setId":11,"channelItemList":{"_bValue":0,"value":[{"id":"vhuyajdqs","name":"绝地求生","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":2,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"舞蹈","cover":"http://v-huya-pic.huya.com/vhuya/76bfcc2a-c297-4e2c-8e06-b2afad7bd25d.png","vid":0,"setId":51,"channelItemList":{"_bValue":0,"value":[{"id":"shixialiuxing","name":"时下流行","_classname":"HUYAVIDEO.ChannelItem"},{"id":"Dance","name":"主播热舞","_classname":"HUYAVIDEO.ChannelItem"},{"id":"jiewu","name":"街舞","_classname":"HUYAVIDEO.ChannelItem"},{"id":"guofengwu","name":"国风舞","_classname":"HUYAVIDEO.ChannelItem"},{"id":"zhaiwu","name":"宅舞","_classname":"HUYAVIDEO.ChannelItem"},{"id":"wudaojiaoxue","name":"舞蹈教学","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"搞笑","cover":"http://v-huya-pic.huya.com/vhuya/94e32b54-bc91-41d2-b353-314e6b9f02d4.png","vid":0,"setId":49,"channelItemList":{"_bValue":0,"value":[{"id":"egaozhengu","name":"恶搞整蛊","_classname":"HUYAVIDEO.ChannelItem"},{"id":"tucao","name":"吐槽","_classname":"HUYAVIDEO.ChannelItem"},{"id":"tiaozhan","name":"挑战","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"影视","cover":"http://v-huya-pic.huya.com/vhuya/a76dd328-b980-41da-875a-68ae11399ee0.png","vid":0,"setId":29,"channelItemList":{"_bValue":0,"value":[{"id":"duanju","name":"短剧","_classname":"HUYAVIDEO.ChannelItem"},{"id":"Commentary","name":"影评解说","_classname":"HUYAVIDEO.ChannelItem"},{"id":"yp","name":"片段","_classname":"HUYAVIDEO.ChannelItem"},{"id":"xspd","name":"预告片","_classname":"HUYAVIDEO.ChannelItem"},{"id":"cut","name":"混剪","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"美食","cover":"http://v-huya-pic.huya.com/vhuya/e9ab48d2-a581-4862-9617-cd29a8ba3d73.png","vid":0,"setId":37,"channelItemList":{"_bValue":0,"value":[{"id":"cooking","name":"美食制作","_classname":"HUYAVIDEO.ChannelItem"},{"id":"eating","name":"吃播","_classname":"HUYAVIDEO.ChannelItem"},{"id":"meishitiaozhan","name":"美食挑战","_classname":"HUYAVIDEO.ChannelItem"},{"id":"meishitandian","name":"美食探店","_classname":"HUYAVIDEO.ChannelItem"},{"id":"huwiayeshi","name":"户外野食","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"户外","cover":"http://v-huya-pic.huya.com/vhuya/08a03577-857a-46fb-b223-23b0eab6a283.png","vid":0,"setId":33,"channelItemList":{"_bValue":0,"value":[{"id":"diaoyu","name":"钓鱼","_classname":"HUYAVIDEO.ChannelItem"},{"id":"ganhai","name":"赶海","_classname":"HUYAVIDEO.ChannelItem"},{"id":"luying","name":"露营","_classname":"HUYAVIDEO.ChannelItem"},{"id":"fangchechuxing","name":"房车出行","_classname":"HUYAVIDEO.ChannelItem"},{"id":"meijing","name":"美景","_classname":"HUYAVIDEO.ChannelItem"},{"id":"xiangyeshenghuo","name":"乡野生活","_classname":"HUYAVIDEO.ChannelItem"},{"id":"wanle","name":"玩乐","_classname":"HUYAVIDEO.ChannelItem"},{"id":"survival","name":"探险挑战","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"二次元","cover":"http://v-huya-pic.huya.com/vhuya/d1beaf0f-e4be-4ab8-8271-b564c9bae228.png","vid":0,"setId":35,"channelItemList":{"_bValue":0,"value":[{"id":"cosplay","name":"cosplay","_classname":"HUYAVIDEO.ChannelItem"},{"id":"donghua","name":"动画","_classname":"HUYAVIDEO.ChannelItem"},{"id":"singer","name":"唱见","_classname":"HUYAVIDEO.ChannelItem"},{"id":"seiyuu","name":"声优","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"汽车","cover":"https://v-huya-pic.huya.com/vhuya/5966d764-c7d2-418d-a7db-17959a795d3b.png","vid":0,"setId":71,"channelItemList":{"_bValue":0,"value":[{"id":"car","name":"汽车评测","_classname":"HUYAVIDEO.ChannelItem"},{"id":"qichewenhua","name":"汽车文化","_classname":"HUYAVIDEO.ChannelItem"},{"id":"qicheyanghu","name":"汽车养护","_classname":"HUYAVIDEO.ChannelItem"},{"id":"jiaotonganquan","name":"交通安全","_classname":"HUYAVIDEO.ChannelItem"},{"id":"jiashijiqiao","name":"驾驶技巧","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"知识","cover":"https://v-huya-pic.huya.com/vhuya/a472c2b2-5616-43b1-9788-389288c52870.png","vid":0,"setId":63,"channelItemList":{"_bValue":0,"value":[{"id":"popularscience","name":"日常科普","_classname":"HUYAVIDEO.ChannelItem"},{"id":"qinggan","name":"情感","_classname":"HUYAVIDEO.ChannelItem"},{"id":"kexue","name":"科学","_classname":"HUYAVIDEO.ChannelItem"},{"id":"finance","name":"财经","_classname":"HUYAVIDEO.ChannelItem"},{"id":"zhiyezhichang","name":"职业职场","_classname":"HUYAVIDEO.ChannelItem"},{"id":"education","name":"校园学习","_classname":"HUYAVIDEO.ChannelItem"},{"id":"jiatingjiaoyu","name":"家庭教育","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"主机游戏","cover":"http://v-huya-pic.huya.com/vhuya/66f8eaea-1cb3-480d-ba1f-a9d7bc6ab4f7.png","vid":0,"setId":6,"channelItemList":{"_bValue":0,"value":[{"id":"vhuyamc","name":"我的世界","_classname":"HUYAVIDEO.ChannelItem"},{"id":"consolegame","name":"主机游戏","_classname":"HUYAVIDEO.ChannelItem"},{"id":"dlyx","name":"独立游戏","_classname":"HUYAVIDEO.ChannelItem"},{"id":"KOF","name":"格斗游戏","_classname":"HUYAVIDEO.ChannelItem"},{"id":"nostalgicgame","name":"怀旧游戏","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"网络游戏","cover":"http://v-huya-pic.huya.com/vhuya/7d41be9b-c673-49d6-b153-c41cb480bc25.png","vid":0,"setId":4,"channelItemList":{"_bValue":0,"value":[{"id":"blzy","name":"堡垒之夜","_classname":"HUYAVIDEO.ChannelItem"},{"id":"vhuyadnf","name":"地下城与勇士","_classname":"HUYAVIDEO.ChannelItem"},{"id":"vhuyablizzard","name":"守望先锋","_classname":"HUYAVIDEO.ChannelItem"},{"id":"vhuyacf","name":"穿越火线","_classname":"HUYAVIDEO.ChannelItem"},{"id":"vhuyaqpxx","name":"棋牌休闲","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"},{"title":"手机游戏","cover":"http://v-huya-pic.huya.com/vhuya/8f3988d4-a89e-4bc5-a5c6-6f8cb4075afb.png","vid":0,"setId":3,"channelItemList":{"_bValue":0,"value":[{"id":"dxcyyssy","name":"地下城与勇士手游","_classname":"HUYAVIDEO.ChannelItem"},{"id":"yrzx","name":"一人之下","_classname":"HUYAVIDEO.ChannelItem"},{"id":"hyld","name":"荒野乱斗","_classname":"HUYAVIDEO.ChannelItem"},{"id":"cfm","name":"CF手游","_classname":"HUYAVIDEO.ChannelItem"}],"_classname":"list<HUYAVIDEO.ChannelItem>"},"level":1,"url":"","_classname":"HUYAVIDEO.RecommendItem"}],"videoData":{"vid":913068246,"liveId":"","videoTitle":"叫白衣服的小姐姐,再来一次他不香吗?","brief":"叫白衣服的小姐姐,再来一次他不香吗?","sourceClient":106,"channelId":"spoof","videoType":1,"uploadStartTime":1693824711,"uploadEndTime":1693824717,"covers":{"cover":"http://v-huya-img2.msstatic.com/screenshot/2336/913068246/99.jpg?x-oss-process=style/640x360&v=7fed571e1b969ff87373e498d29e34bf1693824737","cover320":"http://v-huya-img2.msstatic.com/screenshot/2336/913068246/99.jpg?x-oss-process=style/320x180&v=7fed571e1b969ff87373e498d29e34bf1693824737","cover375":"http://v-huya-img2.msstatic.com/screenshot/2336/913068246/99.jpg?x-oss-process=style/375x375&v=7fed571e1b969ff87373e498d29e34bf1693824737","cover640":"http://v-huya-img2.msstatic.com/screenshot/2336/913068246/99.jpg?x-oss-process=style/640x360&v=7fed571e1b969ff87373e498d29e34bf1693824737","tuijianCover":"","originCover":"http://v-huya-img2.msstatic.com/screenshot/2336/913068246/99.jpg","firstFrameCover":"https://v-huya-img2.msstatic.com/screenshot/2336/913068246/1.jpg?x-oss-process=image/resize,limit_0,m_lfit,w_640/sharpen,80/format,jpg/interlace,1/quality,q_90","_classname":"HUYAVIDEO.VideoCover"},"tags":"美女","playNums":798453,"favorNums":0,"shareNums":0,"commentNums":0,"danmuNums":0,"videoUrl":"http://v.huya.com/play/913068246.html","duration":"00:17","updateTime":1693828880,"userInfo":{"uid":"1199626175454","userNick":"素素电影","avatar":"https://huyaimg.msstatic.com/avatar/1089/3a/9a061858ce500f2754870ca55a8c0a_180_135.jpg?1680837126","homepage":"http://v.huya.com/u/1199626175454","isOnline":false,"lastLiveTime":0,"roomId":0,"_classname":"HUYAVIDEO.VideoUserInfo"},"videoDefinitions":{"_bValue":0,"value":[{"sSize":"2934281","sWidth":"1080","sHeight":"1080","sDefinition":"yuanhua","sUr…
# 正则表达式的写法这个json是不完整的表达式。
#https://liveapi.huya.com/moment/getMomentContent?callback=jQuery1124019496731761586972_1694838757803&videoId=&_=1694838757810
#https://liveapi.huya.com/moment/getCommentList?callback=&uid=&Id={n}&_=1694784083553 # 对链接分析就是重点。
#https://liveapi.huya.com/moment/getMomentContent?callback=&videoId={n}&uid=&_=1690430766941
# re_findall = re.findall(ex, r)[0]
# #这个是json字符串的数据类型。
# findall_replace= re_findall.replace(')', '')
#把json字符串转为字典数据。# 突破性思维!!! 就是在详情页上面加上一个分页的操作,https://liveapi.huya.com/moment/getMomentContent?callback=&videoId={n}&uid=&_=1690430766941 类似于这样的。url_ = requests_get.json()['data']['moment']['videoInfo']['definitions'][0]['url']headers__content = requests.get(url=url_, headers=headers).contentwith open('虎牙搞笑视频\\' +title_ + '.mp4', mode='wb', )as f:print("正在下载",title_)f.write(headers__content)#j_data_ = j['data']['moment']['videoInfo'][ 'definitions'][0]['url']#ex = 'href="(.*?)" ' # 文件中还有href的文件。
#e=re.findall(ex,headers__text)
#print(e)
以上是爬取的代码
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!