赞
踩
目录
不要问,问就是博主喜欢,嘿嘿嘿嘿(流口水)
- # 请求b站视频
- import json
- import requests
- from lxml import etree
-
- if __name__ == '__main__':
- head = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"
- ,
- # 防盗链
- "Referer": "https://www.bilibili.com/"
- ,
- "Cookie": "buvid3=3EEF3EE1-472C-F430-105F-0E4F321F331C25161infoc; b_nut=1720613925; _uuid=46847D108-EFD1-9828-99910-32119EADB6EB26022infoc; enable_web_push=DISABLE; buvid4=E9918A3A-008D-2748-2B1F-78B04E8CCA5825688-024071012-IYJQtQw8DTdtrI0uY1UGvQ%3D%3D; buvid_fp=adf3cfaf35396cd4f051041d58ea252d; DedeUserID=455536180; DedeUserID__ckMd5=ece5cba51b3582b0; header_theme_version=CLOSE; rpdid=|(Y|RJRRJ~m0J'u~k|YuR|k); hit-dyn-v2=1; CURRENT_BLACKGAP=0; CURRENT_FNVAL=4048; CURRENT_QUALITY=80; b_lsid=FA4254F2_190DA67F5A6; bmg_af_switch=1; bmg_src_def_domain=i0.hdslb.com; SESSDATA=464ec009%2C1737203167%2C4093c%2A72CjCcAmS6QdnftcRgMbranRB57RDcvOgVEYQ2eANilbiZTke2ujiipqzyDhLaHzdf-lQSVnBETDB6RC1iRTBPOXE0MXJvWndtQ2J5M0ttRUdsNzV4eDBOS0ZHSnJFNGstUXdoZV9YWlFDMUN6WktnVWdyZzNXTHoxX3ZHMEQ4WnBRVFhuS3FWdlZRIIEC; bili_jct=7dcc1afe4e9061803755fe502bd89493; home_feed_column=5; browser_resolution=1528-750; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjE5MTAzODcsImlhdCI6MTcyMTY1MTEyNywicGx0IjotMX0.ZBTc3IhjtIXKw31_mMRIL58EBBFttxFrh9PXvDeNu74; bili_ticket_expires=1721910327; sid=5oumv249; bp_t_offset_455536180=956974561008549888"
- }
-
- url = "https://www.bilibili.com/video/BV1Ya411x7YS/?spm_id_from=333.788.recommend_more_video.-1&vd_source=c37b332e3068ca57bd61f05c52b41922"
-
- response = requests.get(url, headers=head)
-
- vid = response.text
-
- tree = etree.HTML(vid)
-
- with open("bilibili.html", "w", encoding="utf8") as bili:
- bili.write(vid)
-
- vid_info = "".join(tree.xpath("//head/script[4]/text()"))[20:]
- # print(vid_info) # 是一个json大字符串
-
- info_dict = json.loads(vid_info)
- # print(info_dict) # 变成字典
-
- video_url = info_dict["data"]["dash"]["video"][0]["baseUrl"] # 获取视频的网址
- audio_url = info_dict["data"]["dash"]["audio"][0]["baseUrl"] # 获取音频的网址
-
- video_content = requests.get(video_url, headers=head).content
- audio_content = requests.get(audio_url, headers=head).content
-
- with open("zhangruonan.wmv", "wb") as a:
- a.write(video_content)
-
- with open("zhangruonan.mp4", "wb") as b:
- b.write(audio_content)
-
- pass
可以看见,这次代码多了不少东西,还有些变得不一样了,没事,我们一步步分析。
这次UA伪装的头部代码多了不少,"Cookie"和"Referer",这些其实都是b站的反扒机制。而且这次也不用随机生成UA了,而是使用检查里的,因为随机生成的可能是APP端的,这个用不了。
在该界面按f12进入检查界面,若是检查界面没东西的话就刷新网页,然后往上翻到第一个,复制url粘贴到pycharm即可
流水的url,铁打的发送请求。
response = requests.get(url, headers=head)
作者的废话:
章若楠太好看啦!
封面图片:
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。