如何在有声读物网站上捕获mp3文件的请求url?

2024-09-28 01:26:53 发布

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

网址:

https://www.ting22.com/ting/659-2.html

我想从上面的网站上买一些有声读物。换句话说,我想把有声读物的MP3文件从659-2.html下载到659-1724.html。你知道吗

通过使用F12工具,在[Network]->;[Media]中,我可以看到MP3文件的请求URL,但我不知道如何使用脚本获取URL。你知道吗

以下是我使用的一些规格:

  • 系统:Windows 7 x64
  • Python:3.7.0版

更新:

例如,通过使用F12工具,我可以看到文件的url是“http://audio.xmcdn.com/group58/M03/8D/07/wKgLc1zNaabhA__WAEJyyPUT5k4509.mp3

但我不知道如何在代码中获取MP3文件的URL?而不是如何下载文件。你知道吗

我应该用哪个图书馆?你知道吗

谢谢你。你知道吗


Tags: 文件工具httpscomurl网站htmlwww
1条回答
网友
1楼 · 发布于 2024-09-28 01:26:53

更新

因为请求包不会返回.mp3源代码,所以需要使用Selenium。这是一个经过测试的解决方案:

from selenium import webdriver  # pip install selenium
import urllib3
import shutil
import os


if not os.path.exists(os.getcwd()+'/mp3_folder'):
    os.mkdir(os.getcwd()+'/mp3_folder')


def downloadFile(url=None):
    filename = url.split('/')[-1]
    c = urllib3.PoolManager()
    with c.request('GET', url, preload_content=False) as resp, open('mp3_folder/'+filename, 'wb') as out_file:
        shutil.copyfileobj(resp, out_file)
    resp.release_conn()


driver = webdriver.Chrome('chromedriver.exe')  # download chromedriver from here and place it near the script: https://chromedriver.storage.googleapis.com/72.0.3626.7/chromedriver_win32.zip
for i in range(2, 1725):
    try:
        driver.get('https://www.ting22.com/ting/659-%s.html' % i)
        src = driver.find_element_by_id('mySource').get_attribute('src')
        downloadFile(src)
        print(src)
    except Exception as exc:
        print(exc)

相关问题 更多 >

    热门问题