• 网盘链接文件下载,保存到本地


    import requests
    import os
    import sys
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    # 下载百度云文件
    # 如果过期请自己手动更换
    cookie = "PSTM=1587863792; BIDUPSID=528CEBCD580978A65D8183F0F8D178C5; BAIDUID=4F866DED89BD4C5828831B99910EA8C0:SL=0:NR=10:FG=1; PANWEB=1; MCITY=-%3A; pan_login_way=1; BDUSS=lJrMGpGVHVueGQxQ2VDb3VabHlHMHVwVWxROE1pQ1c0NEdPWk5Semg3MlZqNUZmSVFBQUFBJCQAAAAAAAAAAAEAAAC2C8h7v-zA1sDWaGFoYQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJUCal-VAmpfN; BDUSS_BFESS=lJrMGpGVHVueGQxQ2VDb3VabHlHMHVwVWxROE1pQ1c0NEdPWk5Semg3MlZqNUZmSVFBQUFBJCQAAAAAAAAAAAEAAAC2C8h7v-zA1sDWaGFoYQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJUCal-VAmpfN; STOKEN=4b10bae481fb22c2568ca13de94f39831c5dbe1f6f5e2abbadd6733054c0cbff; SCRC=d536a0d863e50542b5a952cf03d0b973; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BDCLND=9%2Fg%2FRjt87wBhH7GQzysO1WrlA0vePGJ5ubVsd03N29I%3D; delPer=0; PSINO=1; H_PS_PSSID=32820_32617_1420_31660_32706_32230_7516_32115_32718_22157; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1602310772,1602310795,1602310922,1602311003; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1602311003; PANPSC=18386959497552224725%3AKkwrx6t0uHA8Ci%2BUotfp7Tuv496EomYGclEm%2BBBYRJpxJdPFNyV%2FWHwIyfKULKsKCOfgdHnp149Xegu8YAJYaBQRAggk1W3ri9SM%2B9wn5ca5sBnTZnAP1GT%2FMK3iZvOokLmws8hilRGrRlCzpJud7l9l5VLCkYtGsG7YZLnfZ%2BWv%2BjYtLTMR3kI0NZhR03fFueSOQr2pfpJ9bfFV5UX2VQ%3D%3D"
    # 本地谷歌数据
    user_data_dir = r"user-data-dir=C:UsersAdministratorAppDataLocalGoogleChromeUser Data"
    
    
    class Bd_method():
    
        def __init__(self, filename, path):
            # 文件名
            self.filename = filename
            # 文件夹
            self.path = path
            self.s = requests.session()
            self.file_path = os.path.join(path, filename).replace("/", "%2F").replace("\", "%2F")
            # 下载地址
            self.Download_addres = 'https://pcs.baidu.com/rest/2.0/pcs/file?method=download&app_id=309847&path={}'.format(self.file_path)
            # 下载请求头
            self.headers = {
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36",
                # "Host":"pan.baidu.com",
                "Cookie": cookie
            }
    
        # 下载
        def download_file(self):
            """
            :param filename:百度网盘文件名
            :param path:百度网盘路径“/xxx/xxx/“
            :return:
            """
    
            # 如果文件存在,获取文件大小,断点续传继续下载
            # 剩余下载长度 默认-1 表示没下载过
            remaining_byte = -1
            # 默认文件大小为0
            first_byte = 0
            # 获取文件长度
            response = self.s.get(self.Download_addres, headers=self.headers, stream=True)
            # 判断cookie是否已过期
            if response.status_code!=200:
                if response.status_code == 404:
                    print('文件不存在')
                else:
                    print("cookie过期")
                return False
            # 获取文件大小
            response_byte = response.headers['content-length']
            print("本次下载的文件大小为:", response_byte)
            if os.path.exists(self.filename):
                # 当前文件的大小
                first_byte = os.path.getsize(self.filename)
                print("当前文件大小:", first_byte)
                # 剩余文件长度
                remaining_byte = int(response_byte) - int(first_byte)
                print("剩余下载大小", int(remaining_byte))
            # 判断文件是否下载过
            if remaining_byte == 0:
                print("文件已下载过")
                return self.filename
            self.headers['Range'] = "bytes={}-{}".format(first_byte, response_byte)
            r = self.s.get(self.Download_addres, headers=self.headers, stream=True)
            # 写入文件
            with open(self.filename, "ab")as f:
                # 每次写入512字节
                for chunk in r.iter_content(chunk_size=1024):
                    if chunk:
                        first_byte += len(chunk)
                        f.write(chunk)
                        f.flush()
                        ###这是下载实现进度显示####
                        done = int(50 * int(first_byte) / int(response_byte))
                        sys.stdout.write("
    [%s%s] %d%%" % ('' * done, ' ' * (50 - done), 100 * int(first_byte) / int(response_byte)))
                        sys.stdout.flush()
            print("")
            # return self.filename
            if int(first_byte) == int(response_byte):
                return True
            else:
                return False
    
    # 保存
    def save_file(file_url, password, user_data):
        # 配置调起保存数据的浏览器
        # os.system()
        chrome_options = Options()
        chrome_options.add_argument(user_data)
        driver = webdriver.Chrome(chrome_options=chrome_options)
        # 输入网址
        driver.get(file_url)
        try:
            # 输入密码
            driver.find_element_by_xpath('//input[@id="accessCode"]').send_keys(password)
            # 点击【提取文件】
            driver.find_element_by_xpath('//div[@id="submitBtn"]//span[@class="text"]').click()
        except:
            pass
        # 隐式等待10s
        driver.implicitly_wait(10)
        # 获取文件名称
        pan_file_name = driver.find_element_by_xpath('//h2[@class="file-name"]').text
        # print(pan_file_name)
        # 拼接文件在网盘中的路径
        pan_file_path = "/"
        # print(pan_file_path)
        # 点击【保存到网盘】
        driver.find_element_by_xpath('//span[text()="保存到网盘"]').click()
        # 点击【确定】
        driver.find_element_by_xpath('//span[text()="确定"]').click()
        print(pan_file_name + "文件保存成功")
        # 关闭浏览器
        driver.close()
        return pan_file_name, pan_file_path
    
    if __name__ == '__main__':
        # 文件链接
        save_file_path = 'https://pan.baidu.com/s/1NfxRt2tlqbA2mSogWZnC4w'
        # 提取码
        save_file_password = 'w4bu'
        # 获取文件名,网盘中的路径
        pan_file_name, pan_file_path = save_file(save_file_path, save_file_password, user_data_dir)
        # 下载文件
        bd = Bd_method(pan_file_name, pan_file_path)
        result = bd.download_file()
        print(result)
  • 相关阅读:
    精通搜索分析
    简析MonoTouch工作原理
    第二次结对编程作业
    第02组 团队Git现场编程实战
    第一次结对编程作业
    团队项目需求分析报告
    第一次个人编程作业
    软工第一次作业
    团队项目选题报告
    Rookey.Frame之数据库及缓存配置
  • 原文地址:https://www.cnblogs.com/lnd-blog/p/14445729.html
Copyright © 2020-2023  润新知