基于Python爬取愛奇藝資源過程解析
像iqiyi這種視頻網站,現在下載視頻都需要下載相應的客戶端。那么如何不用下載客戶端,直接下載非vip視頻?
選擇你想要爬取的內容
該安裝的程序以及運行環境都配置好
下面這段代碼就是我在愛奇藝里搜素“英文名”,然后出來的視頻,共有20頁,那么我們便從第一頁開始,解析網頁,然后分析
分析每一頁網址,找出規律就可以直接得到所有頁面
然后根據每一個視頻的URL的標簽,如’class’ ’div’ ’href’......通過bs4庫進行爬取
而其他的信息則是直接循環所爬取到的URL,在每一個里再通過標簽去找
import requestsimport pandas as pdfrom 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頁,根據每頁的網址變換規律進行拼接 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)#爬取發布時間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)
上面的代碼輸出結果便是英文名的所有網址及其視頻中的一些信息
這里我需要講一下的是,為什么在爬取作者信息的模塊里我采取了try的方法,因為在我爬取的過程中我發現,有的視頻的上傳作者在視頻左下方,有的在視頻的右下方,有的視頻干脆沒有上傳作者。
同樣的,你想要爬取其他內容也可以用這種方法獲取URL和他的其他信息
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
