如何下载文件而不是带请求的HTML

2024-09-27 04:16:29 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,所以我决定我想要一个程序来下载基于地图编号的osu地图(因为没有更好的术语)。在对链接做了一些测试以理解重定向之后,我得到了一个进入.../download页面的程序-当我到达上述页面时,地图将被下载。然而,当试图通过请求下载它时,我得到了HTML。你知道吗

    def grab(self, identifier=None):
        if not identifier:
            print("Missing Argument: 'identifier'")
            return
        mapLink = f"https://osu.ppy.sh/beatmaps/{identifier}"
        dl = requests.get(mapLink, allow_redirects=True)
        if not dl:
            print("Error: map not found!")
            return
        mapLink2 = dl.url 
        mapLink2 = f"https://osu.ppy.sh/beatmapsets/{self.parseLink(mapLink2)}/download"
        dl = requests.get(mapLink2)

        with open(f"{identifier}.osz", "wb") as f:
            f.write(dl.content)

如果有必要,这里是self.parseLink


    def parseLink(self, mapLink=None):
        if not mapLink:
            return None
        id = mapLink.replace("https://osu.ppy.sh/beatmapsets/","")
        id = id.split("#")
        return id[0]

理想情况下,当我在grab()末尾打开文件时,它应该保存一个可用的.osz文件—一个不是html的文件,可以拖到实际的游戏中使用。当然,这在我的测试中还处于非常早期的阶段,为了方便起见,我将想出一种方法使文件名成为歌曲名。你知道吗

编辑:一个identifier的例子是:OsuMaps().grab("1385415"),以防您想要测试


Tags: httpsselfnoneidreturnif地图not
1条回答
网友
1楼 · 发布于 2024-09-27 04:16:29

有一种非常快速的方法:

  • 需要登录
  • 需要特定元素

此解决方法以https://bloodcat.com/osu/的形式出现—要直接获得地图的下载链接,您只需:https://bloodcat.com/osu/s/<beatmap set number>。你知道吗

举个例子:

id = "653534" # this map is ILY - Panda Eyes 
mapLink = f"https://bloodcat.com/osu/s/{id}" # adds id to the link
dl = requests.get(mapLink)
if len(dl.content) > 330: # see below for explanation
    with open(f"{lines[i].rstrip()}.osz", "wb") as f:
    f.write(dl.content)
else:
    print("Map doesn't exist")

if len(dl.conetent) > 330是我的解决方法,指向一个不起作用的链接。.osz文件可以包含成千上万行未知字符,而站点的“未找到”页面只有不到330行-我们可以用它来检查文件是否太短而不是beatmap。你知道吗

就这些!如果您愿意,可以随意使用代码。你知道吗

相关问题 更多 >

    热门问题