Patrick O’Shaughnessy 主持了一檔全世界最著名的投資類播客,播客名字叫 Invest Like the Best。 内容是關于投資觀念、理财方法、人生規劃等。每期嘉賓會分享其擅長的投資政策和産品,還會推薦書籍。
其官網上可以下載下傳全部播客音頻:https://investlikethebest.libsyn.com
怎麼利用ChatGPT來批量下載下傳呢?
首先看這些播客音頻檔案,都在class="libsyn-item"的div标簽裡面;
然後找到播客檔案标題:
<div class="libsyn-item-title">
<a href="https://investlikethebest.libsyn.com/scott-davis-rob-wertheimer-lessons-from-the-industrial-titans-invest-like-the-best-ep324">Scott Davis & Rob Wertheimer - Lessons from the Industrial Titans - [Invest Like the Best, EP.324]</a>
播客音頻Mp3也很容易找到:
<div class="libsyn-item-content">
<ul class="libsyn-item-free">
<li>
<a href="https://traffic.libsyn.com/secure/investlikethebest/EP.324_-_Lessons_from_Titans_FINAL.mp3">Download this Episode</a></li>
</ul>
</div>
網站一共有17頁,每頁位址很有規律:
https://investlikethebest.libsyn.com/page/1/size/25 第1頁
https://investlikethebest.libsyn.com/page/2/size/25 第2頁
https://investlikethebest.libsyn.com/page/17/size/25 17頁
現在,可以在ChatGPT中輸入提示詞了:
你是一個Python程式設計專家,要完成一個下載下傳網頁音頻檔案的任務,具體步驟如下:
打開網頁:https://investlikethebest.libsyn.com/page/{pagenumber}/size/25
其中,參數pagenumber的值是從1到17;
定位class="libsyn-item"的div标簽;
在這個div标簽内定位class="libsyn-item-title"的div标簽,提取第一個a标簽的内容,作為音頻檔案的标題名;
然後定位div class="libsyn-item-content"的div标簽,提取其中a标簽的href值,作為音頻檔案下載下傳的URL位址;
下載下傳音頻檔案,儲存到電腦D盤的檔案夾:invest-like-the-best
chatgpt生成Python源代碼如下:
import os
import requests
from bs4 import BeautifulSoup
# 建立儲存音頻檔案的檔案夾
if not os.path.exists('D:/invest-like-the-best'):
os.makedirs('D:/invest-like-the-best')
# 循環通路每一頁
for pagenumber in range(1, 18):
url = f'https://investlikethebest.libsyn.com/page/{pagenumber}/size/25'
response = requests.get(url)
# 解析網頁内容
soup = BeautifulSoup(response.text, 'html.parser')
# 定位到每一個音頻項
for item in soup.find_all('div', class_='libsyn-item'):
# 提取音頻标題
title = item.find('div', class_='libsyn-item-title').a.text
# 提取音頻下載下傳連結
download_url = item.find('div', class_='libsyn-item-content').a['href']
print(f'Downloading {title}...')
# 下載下傳音頻檔案
audio_response = requests.get(download_url)
# 儲存音頻檔案
with open(f'D:/invest-like-the-best/{title}.mp3', 'wb') as f:
f.write(audio_response.content)
print(f'Downloaded {title}!')
運作,下載下傳成功: