python動(dòng)態(tài)視頻下載器的實(shí)現(xiàn)方法
這里向大家分享一下python爬蟲(chóng)的一些應(yīng)用,主要是用爬蟲(chóng)配合簡(jiǎn)單的GUI界面實(shí)現(xiàn)視頻,音樂(lè)和小說(shuō)的下載器。今天就先介紹如何實(shí)現(xiàn)一個(gè)動(dòng)態(tài)視頻下載器。
爬取電影天堂視頻
首先介紹的是python爬取電影天堂網(wǎng)站的視頻(包括電影,電視劇,綜藝等),主要是用selenium動(dòng)態(tài)網(wǎng)頁(yè)技術(shù)加上簡(jiǎn)單的爬蟲(chóng)技術(shù)。
(1)電影網(wǎng)站首頁(yè)面地址:https://www.dytt8.net/
(2)用到的技術(shù):selenium模擬瀏覽器運(yùn)行。
(3)首先要安裝配置selenium庫(kù)和不同瀏覽器和該庫(kù)配合的插件。這里安裝配置的過(guò)程略過(guò)。
(4)然后我們用下面的代碼打開(kāi)首頁(yè),并輸出該網(wǎng)頁(yè)的源碼:
def getSource(url): browser = webdriver.Chrome() browser.get(url) print(browser.page_source) browser.close()
(5)然后我們找到搜索對(duì)應(yīng)的網(wǎng)頁(yè)元素標(biāo)簽,以及選擇類(lèi)型和立即搜索按鈕對(duì)應(yīng)的標(biāo)簽。

分別為:



(6)然后我們用下面的代碼把用戶輸入的信息模擬放到瀏覽器上
由于未加載完畢會(huì)進(jìn)入廣告頁(yè)面,因此有需要改進(jìn)的地方,這時(shí)就需要延長(zhǎng)載入時(shí)間。這里有顯示等待和隱式等待,用簡(jiǎn)單的隱式等待即可。
有時(shí)候會(huì)出現(xiàn)錯(cuò)誤,因?yàn)檠谏w的div可能會(huì)在進(jìn)行一些操作后,會(huì)消失,比如頁(yè)面還在loading中。這時(shí)候點(diǎn)擊元素的話,就直接點(diǎn)擊在loading的標(biāo)簽上,所以在這個(gè)操作前可以加個(gè)等待,讓掩蓋的div自行消失后,再等待左側(cè)菜單到可點(diǎn)擊狀態(tài)即可;或者進(jìn)行刷新的操作,此div即可消失,再等待左側(cè)菜單到可點(diǎn)擊狀態(tài)即可。
代碼為:
def putUserMessger(url,this_name,this_type):
'''
:param url: 瀏覽器網(wǎng)址
:param this_name: 需要下載的視頻名
:param this_type: 需要下載的視頻類(lèi)型
'''
this_browser = webdriver.Chrome()
this_browser.implicitly_wait(10)
this_browser.get(url)
# 把下載的視頻名和視頻類(lèi)型進(jìn)行模擬瀏覽器匹配
# 搜索輸入框的標(biāo)簽屬性有name和class,這里用name屬性進(jìn)行獲取
this_browser.find_element_by_name('keyword').send_keys(this_name)
time.sleep(2)
# 選擇類(lèi)型下拉框是html自帶的下拉框,不是input做的假的下拉框
Select(this_browser.find_element_by_name('field')).select_by_visible_text(this_type)
time.sleep(2)
# 點(diǎn)擊立即搜索按鈕,submit就不是單純的單擊,它會(huì)涉及到前后臺(tái)的交互
this_browser.find_element_by_name('Submit').click()
this_browser.close()
def main():
name = input('請(qǐng)輸入視頻名:')
type = input('請(qǐng)選擇類(lèi)型:')
url = 'https://www.dytt8.net/'
putUserMessger(url,name,type)
但是還是會(huì)出現(xiàn)下面的問(wèn)題:
selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input name="Submit" type="Submit" value="立即搜索"> is not clickable at point (702, 220). Other element would receive the click: <div style="width: 1017px; height: 577px;"></div> (Session info: chrome=73.0.3683.86) (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT 10.0.17134 x86_64)
但是我們發(fā)現(xiàn)我們點(diǎn)擊后的其實(shí)是有規(guī)律的,因此用另一個(gè)方法。
(6)二層頁(yè)面配置參數(shù)及視頻三層地址輸出
我們先分析一下url:

分析第二層頁(yè)面地址為:
http://s.ygdy8.com/plus/so.php?typeid=1&keyword=%C4%E3%B5%C4%C3%FB%D7%D6
是由http://s.ygdy8.com/plus/so.php?+typeid=視頻編號(hào)&keyword=視頻名gdk編碼組成。因此需要先轉(zhuǎn)化漢字為網(wǎng)頁(yè)地址url的編碼。
用下面的代碼就可以構(gòu)建一個(gè)需要的網(wǎng)址:
def main():
name = input('請(qǐng)輸入視頻名:')
type = input('請(qǐng)選擇類(lèi)型:')
ret = quote(name, encoding="gbk")
dict = {'電影':'1','電視劇':'2','綜藝':'99','舊綜藝':'89','游戲':'19','動(dòng)漫':'16'}
url = 'http://s.ygdy8.com/plus/so.php?' + 'typeid=' + dict[type] + '&keyword=' + ret
然后我們分析一下網(wǎng)頁(yè):


輸出所有的視頻信息和三級(jí)地址:
def putUserMessger(url):
'''
:param url: 視頻網(wǎng)址
'''
this_browser = webdriver.Chrome()
this_browser.get(url)
# 用css選擇器選擇
input1 = this_browser.find_elements_by_css_selector('.co_content8 ul td a')
for i in input1:
print(i.text)
print(i.get_attribute('href'))
this_browser.close()

(7)三級(jí)網(wǎng)頁(yè)找到下載界面
下載的鏈接的位置是:

然后用request配合pyquery下載即可。
下載鏈接如下:


(8)完整代碼
這里沒(méi)有用到數(shù)據(jù)庫(kù),上面的代碼再配合界面,這里只暫時(shí)沒(méi)有界面的代碼如下:
# encoding: utf-8
from selenium import webdriver
from urllib.request import quote
import requests
from pyquery import PyQuery as pq
from tkinter import *
def putUserMessger(url):
'''
:param url: 視頻網(wǎng)址
'''
last_url = {}
this_browser = webdriver.Chrome()
this_browser.get(url)
# 用css選擇器選擇
input1 = this_browser.find_elements_by_css_selector('.co_content8 ul td a')
for i in input1:
#用字典保存視頻的名字與下載地址
last_url[i.text] = i.get_attribute('href')
this_browser.close()
return last_url
def download(all_url):
this_download = {}
for name,url in dict.items(all_url):
r = requests.get(url)
r.encoding = r.apparent_encoding
doc = pq(r.text)
this_url = doc('#Zoom a')
this_download[name] = this_url.attr('href')
return this_download
type = 0
name = 0
def myRadiobutton():
global type
type = v.get()
def my_all():
name = var.get()
ret = quote(name, encoding="gbk")
url = 'http://s.ygdy8.com/plus/so.php?' + 'typeid=' + str(type) + '&keyword=' + ret
all_url = putUserMessger(url)
result = download(all_url)
print(result)
# 創(chuàng)建一個(gè)主窗口,用于容納整個(gè)GUI程序
root = Tk()
# 設(shè)置主窗口對(duì)象的標(biāo)題欄
root.title("視頻下載器")
L1 = Label(root, text="請(qǐng)選擇類(lèi)型:")
L1.pack(side = TOP)
v = IntVar()
Radiobutton(root, text='電影', variable=v, command=myRadiobutton,value=1).pack(anchor=W)
Radiobutton(root, text='電視劇', variable=v, command=myRadiobutton,value=2).pack(anchor=W)
Radiobutton(root, text='綜藝', variable=v, command=myRadiobutton,value=99).pack(anchor=W)
Radiobutton(root, text='舊綜藝', variable=v, command=myRadiobutton,value=89).pack(anchor=W)
Radiobutton(root, text='游戲', variable=v, command=myRadiobutton,value=19).pack(anchor=W)
Radiobutton(root, text='動(dòng)漫', variable=v, command=myRadiobutton,value=16).pack(anchor=W)
var = StringVar()
L2 = Label(root, text="請(qǐng)輸入視頻名")
L2.pack(side = LEFT)
E1 = Entry(root, bd=5,textvariable=var)
E1.pack(side = RIGHT)
B = Button(root, text="點(diǎn)我",command=my_all).place(x=120, y=80)
# 顯示界面,進(jìn)入主事件循環(huán)
root.mainloop()
結(jié)果如下:



總結(jié)
以上所述是小編給大家介紹的python動(dòng)態(tài)視頻下載器的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Python調(diào)用SMTP服務(wù)自動(dòng)發(fā)送Email的實(shí)現(xiàn)步驟
這篇文章主要介紹了Python調(diào)用SMTP服務(wù)自動(dòng)發(fā)送Email的實(shí)現(xiàn)步驟,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-02-02
淺談python爬蟲(chóng)使用Selenium模擬瀏覽器行為
這篇文章主要介紹了淺談python爬蟲(chóng)使用Selenium模擬瀏覽器行為,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
在Pytorch中自定義dataset讀取數(shù)據(jù)的實(shí)現(xiàn)代碼

