python爬蟲(爬取視頻)


爬蟲爬視頻

爬取步驟

第一步:獲取視頻所在的網頁

第二步:F12中找到視頻真正所在的鏈接

第三步:獲取鏈接並轉換成二進制

第四部:保存

保存步驟代碼

import re
import requests
response =  requests.get('https://vd4.bdstatic.com/mda-jcrx64vi5vct2d2u/sc/mda-jcrx64vi5vct2d2u.mp4?auth_key=1557734214-0-0-d6a29a90222c6caf233e8a2a34c2e37a&bcevod_channel=searchbox_feed&pd=bjh&abtest=all')
video = response.content         #把文件保存成二進制
with open(r'D:\圖片\綠色.mp4','wb') as fw:
    fw.write(video)           #將文件內容寫入該文件
    fw.flush()               #刷新

爬酷6首頁的所有視頻

#有點偷懶變量名用簡單字母啦.............
# https://www.ku6.com/index
# <a class="video-image-warp" target="_blank" href="(.*?)">
#this.src({type: "video/mp4", src: "(.*?)"})
#src({type: "video/mp4", src: "(.*?)"})
import re  # 載入模塊
import requests  # 載入模塊
new_list = []
time = 0
response = requests.get('https://www.ku6.com/index')
data = response.text
# print(data)
url = re.findall('<a class="video-image-warp" target="_blank" href="(.*?)">',data)
for a in url : #type:str
    if a.startswith('/v') or a.startswith('/d'):
        new_list.append(f'https://www.ku6.com{a}')
    elif a.startswith('ht'):
        new_list.append(f"{a.split('垃')[0]}")
for url_1 in new_list:
    response_1 = requests.get(url_1)
    data_1 = response_1.text
    video = re.findall('<source src="(.*?)" type="video/mp4">',data_1) or re.findall('type: "video/mp4", src: "(.*?)"',data_1)
    video_1 = video[0]
    x = video_1.split('/')[-1]
    name = f'{x}.mp4'
    video_response = requests.get(video_1)
    video_3 = video_response.content
    with open(f'D:\圖片\{name}','wb') as fw:
        fw.write(video_3)
        fw.flush()
        time += 1
        print(f'已經爬取{time}個視頻')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM