|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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
所以是什么问题呢?
我看页面直接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()
复制代码
|
|