久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于python爬取梨視頻實(shí)現(xiàn)過程解析

瀏覽:2日期:2022-07-06 08:38:17

目標(biāo)網(wǎng)址:梨視頻

然后我們找到科技這一頁(yè):https://www.pearvideo.com/category_8。其實(shí)你要哪一頁(yè)都行,你喜歡就行。嘿嘿…

這是動(dòng)態(tài)網(wǎng)站,所以咱們直奔network 然后去到XHR:

基于python爬取梨視頻實(shí)現(xiàn)過程解析

找規(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就代表頁(yè)數(shù),讓打開這個(gè)網(wǎng)頁(yè)發(fā)現(xiàn)是靜態(tài)網(wǎng)頁(yè),這最好搞啦,直接上:

基于python爬取梨視頻實(shí)現(xiàn)過程解析

代碼如下:

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...

順利拿到,然后進(jìn)入播放頁(yè)面,卻發(fā)現(xiàn)找不到MP4視頻,怎么辦?經(jīng)過我一番努力(扯掉了幾十根頭發(fā)后)發(fā)現(xiàn),它在另外一個(gè)網(wǎng)址里面

基于python爬取梨視頻實(shí)現(xiàn)過程解析

咋辦?當(dāng)然要想辦法把這個(gè)網(wǎng)址搞到手啦,仔細(xì)分析下,發(fā)現(xiàn)這個(gè)網(wǎng)址非常陌生呀,唯一稍微熟悉點(diǎn)的就是那串?dāng)?shù)字了,前面我們拿到播放頁(yè)的網(wǎng)址后面那串?dāng)?shù)字和這個(gè)對(duì)比,完全是一模一樣的,這樣的話那就好搞了,咱們直接用拼接的方式把它接上去就可以了,看代碼:

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...

出來了,好像稍微有點(diǎn)不一樣,后面那啥&mrd=***************** 沒有,怎么辦?沒有就不要唄,看過我發(fā)的百度圖片那篇的朋友都懂,網(wǎng)址里面有些東西是不需要的,純粹是搞咱們這些玩爬蟲的,惡心咱們。不過沒辦法,畢竟是咱們要去爬人家的數(shù)據(jù)的。

網(wǎng)址問題解決了,但是點(diǎn)進(jìn)去一看,發(fā)現(xiàn)這東東:

基于python爬取梨視頻實(shí)現(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)

基于python爬取梨視頻實(shí)現(xiàn)過程解析

搞定!!

最后我們看一下MP4能不能播放:

基于python爬取梨視頻實(shí)現(xiàn)過程解析

西八!404!!恩,這里就稍微有點(diǎn)麻煩了,還得找數(shù)據(jù),把里面的時(shí)間戳改成 ‘cont-數(shù)字‘,感覺寫了好多,手都有點(diǎn)累了,我就直接上代碼了:

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:])# 提取視頻后面的數(shù)字,數(shù)字是最重要的,需要傳給 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] # 替換視頻里面的時(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)

基于python爬取梨視頻實(shí)現(xiàn)過程解析

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 淮南市| 丰原市| 安国市| 上杭县| 桃江县| 武冈市| 建昌县| 丹阳市| 华坪县| 绥芬河市| 柏乡县| 固原市| 新宾| 贵德县| 武义县| 沂南县| 东丰县| 肇东市| 绥德县| 建始县| 灌南县| 盘锦市| 洞口县| 永新县| 南溪县| 临高县| 抚宁县| 巴楚县| 牙克石市| 陵川县| 尼勒克县| 民丰县| 乌鲁木齐市| 崇义县| 淮滨县| 伊春市| 盐山县| 志丹县| 安国市| 阳高县| 磴口县|