基于python爬取梨視頻實(shí)現(xiàn)過程解析
目標(biāo)網(wǎng)址:梨視頻
然后我們找到科技這一頁:https://www.pearvideo.com/category_8。其實(shí)你要哪一頁都行,你喜歡就行。嘿嘿…
這是動態(tài)網(wǎng)站,所以咱們直奔network 然后去到XHR:
找規(guī)律,這個(gè)應(yīng)該不難,我就直接貼網(wǎng)址上來咯,想要鍛煉的可以找找看哈:
https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=8&start=0
這個(gè)就是我們要找的目標(biāo)網(wǎng)址啦,后面的0就代表頁數(shù),讓打開這個(gè)網(wǎng)頁發(fā)現(xiàn)是靜態(tài)網(wǎng)頁,這最好搞啦,直接上:
代碼如下:
import requests import parsel,re import os target = "https://www.pearvideo.com/videoStatus.jsp?contId=" url = "https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=9&start=0" res = requests.get(url) res.encoding="utf-8" html = parsel.Selector(res.text) lists = html.xpath('/html/body/li/div/a/@href').getall() for each in lists: print("https://www.pearvideo.com/"+each)
output;
https://www.pearvideo.com/video_1703486
https://www.pearvideo.com/video_1703189
https://www.pearvideo.com/video_1703161
https://www.pearvideo.com/video_1702880
https://www.pearvideo.com/video_1702773
...
順利拿到,然后進(jìn)入播放頁面,卻發(fā)現(xiàn)找不到MP4視頻,怎么辦?經(jīng)過我一番努力(扯掉了幾十根頭發(fā)后)發(fā)現(xiàn),它在另外一個(gè)網(wǎng)址里面
咋辦?當(dāng)然要想辦法把這個(gè)網(wǎng)址搞到手啦,仔細(xì)分析下,發(fā)現(xiàn)這個(gè)網(wǎng)址非常陌生呀,唯一稍微熟悉點(diǎn)的就是那串?dāng)?shù)字了,前面我們拿到播放頁的網(wǎng)址后面那串?dāng)?shù)字和這個(gè)對比,完全是一模一樣的,這樣的話那就好搞了,咱們直接用拼接的方式把它接上去就可以了,看代碼:
for each in lists: url_num = each.replace('video_',"") urls = target+url_num print(urls) `` ```python output: https://www.pearvideo.com/videoStatus.jsp?contId=1703486 https://www.pearvideo.com/videoStatus.jsp?contId=1703189 https://www.pearvideo.com/videoStatus.jsp?contId=1703161 https://www.pearvideo.com/videoStatus.jsp?contId=1702880 https://www.pearvideo.com/videoStatus.jsp?contId=1702773 https://www.pearvideo.com/videoStatus.jsp?contId=1702633 ...
出來了,好像稍微有點(diǎn)不一樣,后面那啥&mrd=***************** 沒有,怎么辦?沒有就不要唄,看過我發(fā)的百度圖片那篇的朋友都懂,網(wǎng)址里面有些東西是不需要的,純粹是搞咱們這些玩爬蟲的,惡心咱們。不過沒辦法,畢竟是咱們要去爬人家的數(shù)據(jù)的。
網(wǎng)址問題解決了,但是點(diǎn)進(jìn)去一看,發(fā)現(xiàn)這東東:
恩,很明顯,是遇到反爬機(jī)制了,這個(gè)好搞,要什么給什么就行,代碼如下:
headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36', 'Referer': 'https://www.pearvideo.com/video_'+ str(url_num) } html = requests.get(urls,headers=headers).text print(html)
搞定??!
最后我們看一下MP4能不能播放:
西八!404??!恩,這里就稍微有點(diǎn)麻煩了,還得找數(shù)據(jù),把里面的時(shí)間戳改成 ‘cont-數(shù)字‘,感覺寫了好多,手都有點(diǎn)累了,我就直接上代碼了:
import requests import parsel,re import os target = "https://www.pearvideo.com/videoStatus.jsp?contId=" url = "https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=9&start=0" res = requests.get(url) res.encoding="utf-8" html = parsel.Selector(res.text) lists = html.xpath('/html/body/li/div/a/@href').getall() # print(lists[2:]) # 提取視頻后面的數(shù)字,數(shù)字是最重要的,需要傳給 Referer 和 urls for each in lists: url_num = each.replace('video_',"") urls = target+url_num # print(urls) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36', 'Referer': 'https://www.pearvideo.com/video_'+ str(url_num) } html = requests.get(urls,headers=headers).text cont = 'cont-' + str(url_num) # 提取 mp4 視頻 srcUrl = re.findall(f'"srcUrl":"(.*?)"',html)[0] # 替換視頻里面的時(shí)間戳,改為可以真正播放的數(shù)據(jù) new_url = srcUrl.replace(srcUrl.split("-")[0].split("/")[-1],cont) print(new_url) # 使用視頻后綴當(dāng)視頻名稱 filename = srcUrl.split("/")[-1] # 保存到本地 with open("./images/"+filename,"wb") as f: f.write(requests.get(new_url).content)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
天翼開放平臺免費(fèi)短信驗(yàn)證碼接口使用實(shí)例
天翼開放平臺提供了一個(gè)免費(fèi)的短信驗(yàn)證碼API,下面看看使用方法吧,我們用python開發(fā)接口2013-12-12利用PyQt5模擬實(shí)現(xiàn)網(wǎng)頁鼠標(biāo)移動特效
不知道大家有沒有發(fā)現(xiàn),博客園有些博客左側(cè)會有鼠標(biāo)移動特效。通過移動鼠標(biāo),會形成類似蜘蛛網(wǎng)的特效,本文將用PyQt5實(shí)現(xiàn)這一特效,需要的可以參考一下2022-03-03Python?Generator生成器函數(shù)基本概念及高級用途技巧示例
這篇文章主要為大家介紹了Python?Generator生成器函數(shù)基本概念及高級用途技巧示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12使用Python-OpenCV向圖片添加噪聲的實(shí)現(xiàn)(高斯噪聲、椒鹽噪聲)
這篇文章主要介紹了使用Python-OpenCV向圖片添加噪聲的實(shí)現(xiàn)(高斯噪聲、椒鹽噪聲) ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05TensorFlow自定義模型保存加載和分布式訓(xùn)練
本篇文章將涵蓋 TensorFlow 的高級應(yīng)用,包括如何自定義模型的保存和加載過程,以及如何進(jìn)行分布式訓(xùn)練,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07python print()函數(shù)的end參數(shù)和sep參數(shù)的用法說明
這篇文章主要介紹了python print()函數(shù)的end參數(shù)和sep參數(shù)的用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05