IndexError: list index out of range问题
import reimport 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')
sp_name = li.xpath('./div/a/div/text()')+'.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)
print(sp_url)
报错了 line 20, in <module>
sp_url =re.findall(ex,sp_text)
IndexError: list index out of range
所以是什么问题呢? 看报错能知道是超过了列表范围,看那句的意思,你这里取0都报错,应该是找到的列表为空了。
这里可以加个判断:
sp_ulrs = re.findall(ex,sp_text)
if len(sp_ulrs):
sp_url=sp_urls 我看页面直接get html没有mp4的信息,视频url放在了ajax里。
用ajax拿到:import requests
from lxml import etree
import random
def main():
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'
}
ajax_headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'Host': 'www.pearvideo.com',
'Referer': '',
'X-Requested-With': 'XMLHttpRequest'}
url = 'https://www.pearvideo.com/category_5'
base_url = 'https://www.pearvideo.com/'
r = requests.get(url=url, headers=headers)
html = etree.HTML(r.text)
videos = html.xpath('//li[@class="categoryem"]/div/a/@href')
for video in videos:
mrd = random.random()
video_page = f'https://www.pearvideo.com/videoStatus.jsp?contId={video[-7:]}&mrd={mrd}'
ajax_headers['Referer'] = f'{base_url}{video}'
r = requests.get(video_page, headers=ajax_headers)
video_url = r.json()['videoInfo']['videos']['srcUrl']
print(video, video_url)
if __name__ == '__main__':
main()
页:
[1]