草民网单线程爬取好莱坞大片:草民网单线程爬取好莱坞大片
草民网单线程爬取好莱坞大片:草民网单线程爬取好莱坞大片import socketfrom time import sleep最后贴上代码from urllib import requestimport urllib
兵人 https://www.cmdy5.com/play/21669.html?21669-1-1
打开该界面进行分析
然后F12
发现很多ts文件,我们要得就是他
最后贴上代码
from urllib import request
import urllib
from time import sleep
import socket
import random
class Video():
def __init__(self):
self.headers = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3704.400 QQBrowser/10.4.3615.400"
self.url = ""
def set_url(self i):
if i < 1000:
self.url = "https://zy.baidu-360-yyy-kubo.com/20180705/mBlJPrWZ/800kb/hls/FF9QaL5935d.ts" % i
else:
self.url = "https://zy.baidu-360-yyy-kubo.com/20180705/mBlJPrWZ/800kb/hls/FF9QaL5935d.ts" % i
# 获取并下载ts文件
def download_ts(self i):
rq = request.Request(self.url)
rq.add_header('User-Agent' self.headers)
response = request.urlopen(rq)
res= response.read()
#保存文件路径
with open('./HW/' str(i) ".ts" "wb") as f:
f.write(res)
response.close() # 关闭urlopen方法
def start(self):
for i in range(0 2001):
self.set_url(i)
try:
self.download_ts(i)
print('第' str(i) ".ts" '已下载')
sleep(1)
except urllib.error.URLError as e:
print(e.reason)
break
except socket.timeout as e2:
print(e2.reason)
self.download_ts(i)
if __name__ == '__main__':
Video = Video()
#设置随机请求超时时间
socket.setdefaulttimeout(random.randint(5 10))
video.start()