Python爬取愛奇藝資源


像iqiyi這種視頻網站,現在下載視頻都需要下載相應的客戶端。那么如何不用下載客戶端,直接下載非vip視頻?

選擇你想要爬取的內容

該安裝的程序以及運行環境都配置好

下面這段代碼就是我在愛奇藝里搜素“英文名”,然后出來的視頻,共有20頁,那么我們便從第一頁開始,解析網頁,然后分析

分析每一頁網址,找出規律就可以直接得到所有頁面

然后根據每一個視頻的URL的標簽,如'class' 'div' 'href'......通過bs4庫進行爬取

而其他的信息則是直接循環所爬取到的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頁,根據每頁的網址變換規律進行拼接
    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和他的其他信息


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM