基于python爬取梨視頻實現過程解析
目標網址:梨視頻
然后我們找到科技這一頁:https://www.pearvideo.com/category_8。其實你要哪一頁都行,你喜歡就行。嘿嘿…
這是動態網站,所以咱們直奔network 然后去到XHR:
找規律,這個應該不難,我就直接貼網址上來咯,想要鍛煉的可以找找看哈:
https://www.pearvideo.com/category_loading.jsp?reqType=5&categoryId=8&start=0
這個就是我們要找的目標網址啦,后面的0就代表頁數,讓打開這個網頁發現是靜態網頁,這最好搞啦,直接上:
代碼如下:
import requestsimport parsel,reimport ostarget = '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_1703486https://www.pearvideo.com/video_1703189https://www.pearvideo.com/video_1703161https://www.pearvideo.com/video_1702880https://www.pearvideo.com/video_1702773...
順利拿到,然后進入播放頁面,卻發現找不到MP4視頻,怎么辦?經過我一番努力(扯掉了幾十根頭發后)發現,它在另外一個網址里面
咋辦?當然要想辦法把這個網址搞到手啦,仔細分析下,發現這個網址非常陌生呀,唯一稍微熟悉點的就是那串數字了,前面我們拿到播放頁的網址后面那串數字和這個對比,完全是一模一樣的,這樣的話那就好搞了,咱們直接用拼接的方式把它接上去就可以了,看代碼:
for each in lists: url_num = each.replace(’video_’,'') urls = target+url_num print(urls)`````pythonoutput:https://www.pearvideo.com/videoStatus.jsp?contId=1703486https://www.pearvideo.com/videoStatus.jsp?contId=1703189https://www.pearvideo.com/videoStatus.jsp?contId=1703161https://www.pearvideo.com/videoStatus.jsp?contId=1702880https://www.pearvideo.com/videoStatus.jsp?contId=1702773https://www.pearvideo.com/videoStatus.jsp?contId=1702633...
出來了,好像稍微有點不一樣,后面那啥&mrd=***************** 沒有,怎么辦?沒有就不要唄,看過我發的百度圖片那篇的朋友都懂,網址里面有些東西是不需要的,純粹是搞咱們這些玩爬蟲的,惡心咱們。不過沒辦法,畢竟是咱們要去爬人家的數據的。
網址問題解決了,但是點進去一看,發現這東東:
恩,很明顯,是遇到反爬機制了,這個好搞,要什么給什么就行,代碼如下:
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!!恩,這里就稍微有點麻煩了,還得找數據,把里面的時間戳改成 ‘cont-數字‘,感覺寫了好多,手都有點累了,我就直接上代碼了:
import requestsimport parsel,reimport 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:])# 提取視頻后面的數字,數字是最重要的,需要傳給 Referer 和 urlsfor 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] # 替換視頻里面的時間戳,改為可以真正播放的數據 new_url = srcUrl.replace(srcUrl.split('-')[0].split('/')[-1],cont) print(new_url) # 使用視頻后綴當視頻名稱 filename = srcUrl.split('/')[-1] # 保存到本地 with open('./images/'+filename,'wb') as f: f.write(requests.get(new_url).content)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
