python爬蟲爬取某網(wǎng)站視頻的示例代碼
把獲取到的下載視頻的url存放在數(shù)組中(也可寫入文件中),通過調(diào)用迅雷接口,進(jìn)行自動(dòng)下載。(請先下載迅雷,并在其設(shè)置中心的下載管理中設(shè)置為一鍵下載)
實(shí)現(xiàn)代碼如下:
from bs4 import BeautifulSoup
import requests
import os,re,time
import urllib3
from win32com.client import Dispatch
class DownloadVideo:
def __init__(self):
self.r = requests.session()
self.url=self.get_url()
self.download_urla=[]
self.download_urlb=[]
self.url_set=["%s/shipin/list-短視頻.html"%self.url]
#獲取最新網(wǎng)址
def get_url(self):
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
a=self.r.get('https://www.k58.com',verify=False)
b=a.url
return b
#幾頁內(nèi)容的網(wǎng)址
def url_set1(self,n):
if n==2:
url="%s/shipin/list-短視頻-2.html"%self.url
self.url_set.append(url)
elif n>=3:
m=n+1
for i in range(2,m):
url="%s/shipin/list-短視頻-%d.html"%(self.url,i)
self.url_set.append(url)
else:
pass
#分別加載每一個(gè)頁內(nèi)容的網(wǎng)址
def download_url1(self):
for j in self.url_set:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
r=self.r.get(j,verify=False)
sp1=r.content
soup = BeautifulSoup(sp1, "html.parser")
sp2 = soup.find_all(class_="shown")
for i in sp2:
url1=re.findall('<a href="(.*?)" rel="external nofollow" ',str(i))
u=self.url+url1[0]
self.download_urla.append(u)
#分別獲取各個(gè)視頻的下載鏈接
def download_url2(self):
for i in self.download_urla:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
r=self.r.get(i,verify=False)
sp1=r.content
soup = BeautifulSoup(sp1, "html.parser")
sp2 = soup.find_all(class_="form-control input-sm copy_btn app_disable")
for j in sp2:
url2=j["data-clipboard-text"]
self.download_urlb.append(url2)
#將鏈接寫入txt中
# self.write_txt(url2)
#迅雷下載
def thunder_download(self):
try:
thunder = Dispatch("ThunderAgent.Agent64.1")
for i in self.download_urlb:
thunder.AddTask(i)
thunder.CommitTasks()
time.sleep(2)
except:
print("請下載迅雷,并在其設(shè)置中心的下載管理中設(shè)置為一鍵下載")
def mkdir(self,path):
folder = os.path.exists(path)
if not folder:
os.makedirs(path)
else:
pass
def write_txt(self,c):
self.mkdir(r"D:\AAAAA")
file_name=time.strftime('%Y%m%d_%H%M%S.txt')
with open(r"D:\AAAAA\%s"%file_name,'a') as f:
f.write(c+"\n")
if __name__ == '__main__':
d=DownloadVideo()
#數(shù)字表示幾頁的內(nèi)容
d.url_set1(5)
d.download_url1()
d.download_url2()
d.thunder_download()
到此這篇關(guān)于python爬蟲爬取某網(wǎng)站視頻的示例代碼的文章就介紹到這了,更多相關(guān)python爬蟲爬取網(wǎng)站視頻內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)識(shí)別圖片為文字的示例代碼
這篇文章主要為大家詳細(xì)介紹了Python如何不調(diào)用三方收費(fèi)接口,照樣實(shí)現(xiàn)識(shí)別圖片為文字的功能。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-08-08
Python中venv虛擬環(huán)境超詳細(xì)講解
虛擬環(huán)境是一個(gè)獨(dú)立的Python環(huán)境,它與系統(tǒng)的全局Python環(huán)境隔離,這篇文章主要介紹了Python中venv虛擬環(huán)境的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-04-04
詳解pyinstaller selenium python3 chrome打包問題
這篇文章主要介紹了詳解pyinstaller selenium python3 chrome打包問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
python實(shí)現(xiàn)得到當(dāng)前登錄用戶信息的方法
這篇文章主要介紹了python實(shí)現(xiàn)得到當(dāng)前登錄用戶信息的方法,結(jié)合實(shí)例形式分析了Python在Linux平臺(tái)以及Windows平臺(tái)使用相關(guān)模塊獲取用戶信息的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06
Python實(shí)現(xiàn)對一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)對一個(gè)函數(shù)應(yīng)用多個(gè)裝飾器的方法,結(jié)合實(shí)例形式分析了Python編程中一個(gè)函數(shù)使用多個(gè)裝飾器的簡單操作技巧,需要的朋友可以參考下2018-02-02

