[爬蟲實戰] 如何使用Python 爬蟲 (Python Crawler) 下載Youtube 影片

[爬蟲實戰] 如何使用Python 爬蟲 (Python Crawler) 下載Youtube 影片

 

 

import requests
res = requests.get(‘https://www.youtube.com/watch?v=5yAU52qfYuU’)
print(res.text)
import re
m = re.search(‘layer.config = ({.*?});’,res.text)
print(m.group(1))
import json
jd = json.loads(m.group(1))
print(jd[“args”][“adaptive_fmts”])
import urllib
from urllib.parse import urlparse
parsed = urlparse(jd[“args”][“adaptive_fmts”])
a = urllib.parse.parse_qs(jd[“args”][“adaptive_fmts”])
print(a[‘url’][0])
import shutil
res2 = requests.get(a[‘url’][0], stream = True)
f = open(‘b.mp4’, ‘wb’)
shutil.copyfileobj(res2.raw, f)
f.close()