当前位置: 移动技术网 > IT编程>脚本编程>Python > python使用requests模块下载文件

python使用requests模块下载文件

2020年10月12日  | 移动技术网IT编程  | 我要评论
需要requests模块pip install requests -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com要下载的凡在lst.txt里: 001_怀了你的孩子 002_开Q7的女人 003_我只爱你一个人 004_撞了邪的周末 005_暂且等等 006_他们要结婚了 007_孩子本来就是你的 008_给你的惩罚 009_以德报怨

需要requests模块

pip install requests -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

要下载的凡在lst.txt里:

 001_怀了你的孩子
    002_开Q7的女人
    003_我只爱你一个人
    004_撞了邪的周末
    005_暂且等等
    006_他们要结婚了
    007_孩子本来就是你的
    008_给你的惩罚
    009_以德报怨 

dowload.py

# coding=utf-8

import os
import sys
import requests
import threading
import time


class CDownLoad(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        urlPrefixArr = (
            "http://mp32a.ting89.com:9090",
            "http://mp3-d.ting89.com:9090",
            "http://mp32b.ting89.com:9090",
            "http://mp33a.ting89.com:9090",
            "http://mp3f.ting89.com:9090",
            "http://mp33b.ting89.com:9090",
            "http://mp3-2f.ting89.com:9090",
            "http://mp3-2e.ting89.com:9090",
            "http://mp3-e.ting89.com:9090",
            "http://mp3-f.ting89.com:9090",
            "http://mp3-2.ting89.com:9090",
            "http://mp3d.ting89.com:9090"
        )

        bookTitle = "都市言情/我的26岁女房客"
        names = []
        root = os.getcwd()

        try:
            p = os.path.sep.join((root, "lst.txt"))
            f = open(p, "r", encoding="utf-8")
            while True:
                line = f.readline()
                if (line != ''):
                    n = line.strip()
                    if len(n) > 0:
                        names.append(n)
                else:
                    break
        except Exception as e:
            print("error:: {}".format(e))
            sys.exit(0)
        finally:
            f.close()

        count = 0
        for n in names:
            for prefix in urlPrefixArr:
                urlPath = "/".join((prefix, bookTitle, n)) + ".mp3"
                dstPath = os.path.sep.join((root, n)) + ".mp3"
                count += 1

                print("start {}/{}, {}".format(count, len(names), urlPath))

                if os.path.isfile(dstPath):
                    if (os.path.getsize(dstPath) < 1*1024*1024):
                        os.remove(urlPath)
                        sys.exit(0)

                if (not os.path.isfile(dstPath)):

                    r = requests.get(urlPath)
                    print("r.status {}".format(r.status_code))
                    if (r.status_code == 404):
                        continue

                    with open(dstPath, "wb") as f:
                        f.write(r.content)

                print("ok {}/{}, {}".format(count, len(names), dstPath))
                time.sleep(0.1)

                if (not gIsRun):
                    sys.exit(0)
                    break
                break
        print("success  全部完毕")


if __name__ == "__main__":
    print("xxxxxx-----------------xxxxx")
    t = CDownLoad()
    t.start()
    gIsRun = True

    while True:
        line = input("输入命令:")
        if line == "q":
            gIsRun = False
            break 

将下载的文件改名。如 “141_你好.mp3” 改成 "141.mp3"

# coding=utf-8
import os
import sys

if __name__ == "__main__":

    root = os.getcwd()

    for parent, dirs, fs in os.walk(root):

        for f in fs:
            if f.endswith(".mp3"):
                idx = f.find(".mp3")
                if (f.find("_") != -1):
                    idx = f.find("_")
                prefix = f[0:idx]
                ne = str(int(prefix)) + ".mp3"
                print("old={}, ne={}".format(f, ne))

                oldPath = os.path.sep.join((parent, f))
                newPath = os.path.sep.join((parent, ne))
                try:
                    os.rename(oldPath, newPath)
                except Exception as e:
                    print("error {}".format(e))
                else:
                    pass 

已经下载的文件按照编号排序改名:

# coding=utf-8
import sys
import os
import eyed3
import mutagen
from mutagen.id3 import ID3, TIT2
from mutagen.easyid3 import EasyID3


'''
description: 使用mutagen修改歌曲名,挺好用
param {type} 
return {type} void
'''


def dealMutagen(path):
    fn = os.path.basename(path)
    parentPath = os.path.dirname(path)
    pn = os.path.basename(parentPath)
    realName = pn + "-" + fn

    try:
        meta = EasyID3(path)
    except mutagen.id3.ID3NoHeaderError:
        print(path)
        meta = mutagen.File(path, easy=True)
        meta.add_tags()
        meta.save()

    song = ID3(path)
    # song['TIT2'] = TIT2(
    #     encoding=3,
    #     text=info
    # )

    song["TIT2"] = TIT2(encoding=3, text=realName)
    song.save()


def getMp3Files(parent):
    mp3Files = []

    sons = os.listdir(parent)

    if (isinstance(sons, list)):
        for n in sons:
            # print("{}  {}".format(n, os.sep.join((parent, n))))
            sonPath = os.sep.join((parent, n))

            if (os.path.isdir(sonPath)):
                grandChildren = getMp3Files(sonPath)
                # print("mepath {}, {}".format(sonPath, grandChildren))
                mp3Files.extend(grandChildren)
            elif os.path.isfile(sonPath) and len(sonPath) > 3:
                suffix = sonPath[len(sonPath)-4:]
                if suffix == ".mp3":
                    mp3Files.append(sonPath)
            else:
                pass

    return mp3Files


'''
description: 使用eyed3修改歌曲名,不好用
param {type} 
return {type} void
'''


def dealOneMp3(path, count):

    index = "1000.mp3"

    try:
        audio = eyed3.load(path)
        if (audio.tag is None):
            audio.initTag()

        # print("tag " + audio.tag.title)

        title = audio.tag.title
        fn = os.path.basename(path)
        # parentPath = os.path.dirname(path)
        # parent = os.path.basename(parentPath)
        audio.tag.title = fn
        # audio.tag.title = parent + "_" + fn
        # print("{} {} {}".format(path, parent, fn))
        isError = "空白"
        audio.tag.save()
    except BaseException as identifier:
        print("BaseException ", identifier)
        isError = "错了"
    else:
        isError = "正确"
    finally:
        mp3 = eyed3.load(path)
        title = "none"
        if (not (mp3.tag is None)):
            title = mp3.tag.title

        if (title > index):
            print("{}  {} {} {} {}".format(
                path, title, fn, count, isError))


def dealAll(allPath):
    count = 0
    for p in allPath:
        print("开始 {} {}".format(p, count))
        # dealOneMp3(p, count)
        dealMutagen(p)
        count += 1


def renameAll(root):
    newMp3Paths = []
    oldAll = getMp3Files(root)

    for p in oldAll:
        wans = ["(完).mp3", "(鐎箤).mp3"]
        for wanSuffix in wans:
            dp = os.path.dirname(p)
            fn = os.path.basename(p)

            if fn.endswith(wanSuffix):
                try:
                    newFn = fn[0:len(fn)-len(wanSuffix)] + ".mp3"
                    newPath = os.path.sep.join((dp, newFn))
                    os.rename(p, newPath)
                except Exception as e:
                    print(e)
                    sys.exit(1)
                else:
                    print("rename ok {}".format(p))

    newAll = getMp3Files(root)
    for p in newAll:
        dp = os.path.dirname(p)
        fn = os.path.basename(p)
        oldPrefix = fn[0:len(fn)-len(".mp3")]
        newName = oldPrefix.zfill(4) + ".mp3"
        newPath = os.path.sep.join((dp, newName))
        try:
            os.rename(p, newPath)
        except Exception as e:
            print(e)
            newMp3Paths.append(p)
        else:
            newMp3Paths.append(newPath)

    newMp3Paths.sort(reverse=False)
    return newMp3Paths


if __name__ == "__main__":
    all = renameAll(os.path.abspath("."))
    # print("\n".join(all))
    # print(len(all))

    dealAll(all) 

本文地址:https://blog.csdn.net/wulong710/article/details/109027483

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网