基于Python爬取愛奇藝資源過程解析
像iqiyi這種視頻網(wǎng)站,現(xiàn)在下載視頻都需要下載相應(yīng)的客戶端。那么如何不用下載客戶端,直接下載非vip視頻?
選擇你想要爬取的內(nèi)容
該安裝的程序以及運行環(huán)境都配置好
下面這段代碼就是我在愛奇藝里搜素“英文名”,然后出來的視頻,共有20頁,那么我們便從第一頁開始,解析網(wǎng)頁,然后分析
分析每一頁網(wǎng)址,找出規(guī)律就可以直接得到所有頁面
然后根據(jù)每一個視頻的URL的標簽,如'class' 'div' 'href'......通過bs4庫進行爬取
而其他的信息則是直接循環(huán)所爬取到的URL,在每一個里再通過標簽去找
import requests import pandas as pd from bs4 import BeautifulSoup #爬取URL headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36'} b=[] for i in range(1,2): url="https://so.iqiyi.com/so/q_英文名_ctg_t_0_page_"+str(i)+"_p_1_qc_0_rd__site__m_1_bitrate_" #共20頁,根據(jù)每頁的網(wǎng)址變換規(guī)律進行拼接 r=requests.get(url,headers=headers) soup=BeautifulSoup(r.text,"html.parser") a=soup.findAll('a',{'class':'main-tit'}) for i in a: if 'http://www.'in i.get('href')and 'html'in i.get('href'): b.append(i.get('href')) print(b) #爬取標題 e=[] for k in b: res=requests.get(k,headers=headers) Soup=BeautifulSoup(res.text,'html.parser') c=Soup.findAll('div',{'class':'feed-title-box'}) for d in c: e.append(d.find('h1').text) print(e) #爬取標題下方描述 f=[] for j in b: res=requests.get(j,headers=headers) Soup=BeautifulSoup(res.text,'html.parser') c=Soup.findAll('div',{'class':'qy-play-intro-feed'}) for d in c: f.append(d.find('p',{'class':"intro-iterm__block"}).text) print(f) #爬取發(fā)布時間 h=[] for j in b: res=requests.get(j,headers=headers) Soup=BeautifulSoup(res.text,'html.parser') c=Soup.findAll('div',{'class':'intro-iterm'}) for d in c: ff=(d.find('span',{'class':"intro-iterm__txt"})) if ff==None: continue h.append(ff.text) print(h) # 爬取上傳作者 m=[] for k in b: res=requests.get(k,headers=headers) Soup=BeautifulSoup(res.text,'html.parser') c=Soup.find('div',{'id':'block-P'}) d=Soup.find('div',{'class':'qy-player-maker'}) try: name=c.get(':uploader').split(',')[1].split(':')[1].replace('"','')#輸出是字符串的格式,所以用split切割。replace替換 except: try: name=d.get(':uploader').split(',')[1].split(':')[1].replace('"','') except: m.append("匿名用戶") m.append(name) print(m)
上面的代碼輸出結(jié)果便是英文名的所有網(wǎng)址及其視頻中的一些信息
這里我需要講一下的是,為什么在爬取作者信息的模塊里我采取了try的方法,因為在我爬取的過程中我發(fā)現(xiàn),有的視頻的上傳作者在視頻左下方,有的在視頻的右下方,有的視頻干脆沒有上傳作者。
同樣的,你想要爬取其他內(nèi)容也可以用這種方法獲取URL和他的其他信息
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python+OpenCV圖像處理——打印圖片屬性、設(shè)置存儲路徑、調(diào)用攝像頭
這篇文章主要介紹了Python+OpenCV圖像處理——打印圖片屬性、設(shè)置存儲路徑、調(diào)用攝像頭的示例,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-10-10Python打印scrapy蜘蛛抓取樹結(jié)構(gòu)的方法
這篇文章主要介紹了Python打印scrapy蜘蛛抓取樹結(jié)構(gòu)的方法,實例分析了打印scrapy蜘蛛抓取樹結(jié)構(gòu)的技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04