不能翻页获取视频,咋改啊

import requests
from lxml import etree

headers = {
    'authority': 'ibaotu.com',
    'pragma': 'no-cache',
    'cache-control': 'no-cache',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
    'accept': '*/*',
    'origin': 'https://plus.ibaotu.com',
    'sec-fetch-site': 'same-site',
    'sec-fetch-mode': 'cors',
    'sec-fetch-dest': 'empty',
    'referer': 'https://plus.ibaotu.com/search?category=%E8%A7%86%E9%A2%91&keyword=',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
    'cookie': 'md_session_id=20230719003256124; user_refers=a%3A2%3A%7Bi%3A0%3Bs%3A3%3A%22bwd%22%3Bi%3A1%3Bs%3A13%3A%22www.baidu.com%22%3B%7D; new_edition_type_v4=1; __is_small_screen=1; Hm_lvt_4df399c02bb6b34a5681f739d57787ee=1689776260; Hm_lpvt_4df399c02bb6b34a5681f739d57787ee=1689776260; md_session_expire=1800; FIRSTVISITED=1689776264.704; from_type=; referer=http%3A%2F%2Fibaotu.com%2F%3Fm%3DAjaxSearch%26a%3Ddef; _clck=epszo2|2|fdf|0|1295; _clsk=1kodr2i|1689777324327|5|1|y.clarity.ms/collect',
}
params = {
    'm': 'PlusApi',
    'a': 'search',
    'keyword': '',
    'tabname': '视频',
    'c1': '',
    'c2': '',
    'c3': '',
    'c4': '',
    'format_type': '',
    'width': '',
    'height': '',
    'copyright': '',
    'resolution': '',
    'duration': '',
    'page': '3',
    'size': '80',
    'sort': '',
    'format': '',
    'color': '',
}

response = requests.get(url=f'https://ibaotu.com/shipin/7-0-0-0-0-1.html', headers=headers)  # 发送请求

html_text = response.content.decode('utf-8')  # 编译

html = etree.HTML(html_text)  # 自动修正html文档

src_list = html.xpath('//div[@class="video-play"]/video/@src')  # 视频网址

title_list = html.xpath('//span[@class="video-title"]/text()')  # 视频名称
for src, title in zip(src_list, title_list):
    res = requests.get('https:' + src, headers=headers)
    filename = title + '.mp4'
    print('正在下载视频:' + filename)
    with open('video/' + filename, 'wb') as f:
        f.write(res.content)

请问怎么实现翻页获取多页视频,

img

怀疑你的问题是 f.write 没有使用追加模式,每次把上一次写入的冲掉了
你调试下

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这有个类似的问题, 你可以参考下: https://ask.csdn.net/questions/7555717
  • 这篇博客你也可以参考下:不要轻易当舔狗,除非你想当技术型舔狗
  • 您还可以看一下 郑朝军老师的产品经理思维课程中的 【录播】需求收集_用户访谈小节, 巩固相关知识点
  • 除此之外, 这篇博客: 这也敢爬,你离牢饭不远了,爬虫逆向实战案例中的 部分也许能够解决你的问题。
  • 以下回答由chatgpt基于相关博客总结生成:

    问题分析: 当前代码只获取了第一页的视频信息,需要进行翻页获取多页视频。主要的问题在于参数params中的'page'值没有进行循环变化的设定。

    解决方案: 1. 首先需要获取总页数,以确定翻页的范围。 2. 在循环中进行参数的更新。 3. 修改请求地址,将参数拼接到url中。 4. 在循环中将视频文件保存到本地。

    代码实现如下所示:

    import requests
    from lxml import etree
    
    headers = {
        'authority': 'ibaotu.com',
        'pragma': 'no-cache',
        'cache-control': 'no-cache',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
        'accept': '*/*',
        'origin': 'https://plus.ibaotu.com',
        'sec-fetch-site': 'same-site',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://plus.ibaotu.com/search?category=%E8%A7%86%E9%A2%91&keyword=',
        'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
        'cookie': 'md_session_id=20230719003256124; user_refers=a%3A2%3A%7Bi%3A0%3Bs%3A3%3A%22bwd%22%3Bi%3A1%3Bs%3A13%3A%22www.baidu.com%22%3B%7D; new_edition_type_v4=1; __is_small_screen=1; Hm_lvt_4df399c02bb6b34a5681f739d5778ee=1689776260; Hm_lpvt_4df399c02bb6b34a5681f739d5778ee=1689776260; md_session_expire=1800; FIRSTVISITED=1689776264.704; from_type=; referer=http%3A%2F%2Fibaotu.com%2F%3Fm%3DAjaxSearch%26a%3Ddef; _clck=epszo2|2|fdf|0|1295; _clsk=1kodr2i|1689777324327|5|1|y.clarity.ms/collect',
    }
    
    # 获取总页数
    def get_total_pages():
        response = requests.get(url=f'https://ibaotu.com/shipin/7-0-0-0-0-1.html', headers=headers)
        html_text = response.content.decode('utf-8')
        html = etree.HTML(html_text)
        total_pages = int(html.xpath('//div[@class="pagebar"]/a[last()-1]/text()')[0])
        return total_pages
    
    # 翻页获取视频信息
    def get_videos():
        total_pages = get_total_pages()  # 获取总页数
        for page in range(1, total_pages+1):
            params['page'] = str(page)  # 更新页数参数
            response = requests.get(url=f'https://ibaotu.com/shipin/7-0-0-0-0-{page}.html', headers=headers, params=params)
            html_text = response.content.decode('utf-8')
            html = etree.HTML(html_text)
            src_list = html.xpath('//div[@class="video-play"]/video/@src')
            title_list = html.xpath('//span[@class="video-title"]/text()')
            for src, title in zip(src_list, title_list):
                res = requests.get('https:' + src, headers=headers)
                filename = title + '.mp4'
                print('正在下载视频:' + filename)
                with open('video/' + filename, 'wb') as f:
                    f.write(res.content)
    
    # 调用函数获取视频信息
    get_videos()
    

    这样,就可以通过循环翻页获取多页视频信息,并将视频保存到本地。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^