基于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和他的其他信息

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持来客网。