鱼C论坛

 找回密码
 立即注册
查看: 1130|回复: 2

[已解决]IndexError: list index out of range问题

[复制链接]
发表于 2021-7-29 11:43:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import re
import requests
from lxml import etree
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'
}
url ='https://www.pearvideo.com/category_5'  #当前页面请求
dc_text =requests.get(url=url,headers=headers).text
dc_tree =etree.HTML(dc_text)
dc_li =dc_tree.xpath('//*[@id="listvideoListUl"]/li')
#print(dc_li)
urls =[]
for li in dc_li:
    new_url ='https://www.pearvideo.com/'+li.xpath('./div/a/@href')[0]
    sp_name = li.xpath('./div/a/div[2]/text()')[0]+'.mp4'
    print(new_url,sp_name)
    sp_text =requests.get(url=new_url,headers=headers).text
    #ex ='srcUrl="(.*?)",vdoUrl'
    ex = 'srcUrl="(.*?)",vdoUrl'
    sp_url =re.findall(ex,sp_text)[0]
    print(sp_url)

报错了 line 20, in <module>
    sp_url =re.findall(ex,sp_text)[0]
IndexError: list index out of range

所以是什么问题呢?
最佳答案
2021-7-31 12:00:53
我看页面直接get html没有mp4的信息,视频url放在了ajax里。 Screenshot 2021-07-31 115905.jpg
用ajax拿到:
  1. import requests
  2. from lxml import etree
  3. import random


  4. def main():
  5.     headers = {
  6.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
  7.     }
  8.     ajax_headers = {
  9.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
  10.         'Host': 'www.pearvideo.com',
  11.         'Referer': '',
  12.         'X-Requested-With': 'XMLHttpRequest'}
  13.     url = 'https://www.pearvideo.com/category_5'
  14.     base_url = 'https://www.pearvideo.com/'
  15.     r = requests.get(url=url, headers=headers)
  16.     html = etree.HTML(r.text)
  17.     videos = html.xpath('//li[@class="categoryem"]/div/a/@href')
  18.     for video in videos:
  19.         mrd = random.random()
  20.         video_page = f'https://www.pearvideo.com/videoStatus.jsp?contId={video[-7:]}&mrd={mrd}'
  21.         ajax_headers['Referer'] = f'{base_url}{video}'
  22.         r = requests.get(video_page, headers=ajax_headers)
  23.         video_url = r.json()['videoInfo']['videos']['srcUrl']
  24.         print(video, video_url)


  25. if __name__ == '__main__':
  26.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-29 18:15:36 | 显示全部楼层
看报错能知道是超过了列表范围,看那句的意思,你这里取0都报错,应该是找到的列表为空了。
这里可以加个判断:
  1. sp_ulrs = re.findall(ex,sp_text)
  2. if len(sp_ulrs):
  3.     sp_url=sp_urls[0]
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-7-31 12:00:53 | 显示全部楼层    本楼为最佳答案   
我看页面直接get html没有mp4的信息,视频url放在了ajax里。 Screenshot 2021-07-31 115905.jpg
用ajax拿到:
  1. import requests
  2. from lxml import etree
  3. import random


  4. def main():
  5.     headers = {
  6.         'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
  7.     }
  8.     ajax_headers = {
  9.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
  10.         'Host': 'www.pearvideo.com',
  11.         'Referer': '',
  12.         'X-Requested-With': 'XMLHttpRequest'}
  13.     url = 'https://www.pearvideo.com/category_5'
  14.     base_url = 'https://www.pearvideo.com/'
  15.     r = requests.get(url=url, headers=headers)
  16.     html = etree.HTML(r.text)
  17.     videos = html.xpath('//li[@class="categoryem"]/div/a/@href')
  18.     for video in videos:
  19.         mrd = random.random()
  20.         video_page = f'https://www.pearvideo.com/videoStatus.jsp?contId={video[-7:]}&mrd={mrd}'
  21.         ajax_headers['Referer'] = f'{base_url}{video}'
  22.         r = requests.get(video_page, headers=ajax_headers)
  23.         video_url = r.json()['videoInfo']['videos']['srcUrl']
  24.         print(video, video_url)


  25. if __name__ == '__main__':
  26.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 22:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表