Python实现TeaHour播客音频批量下载脚本
本文介绍了一个使用Python编写的简易脚本,用于从TeaHour.fm网站抓取播客音频下载链接。通过解析网页HTML结构,脚本自动提取音频URL并生成Shell下载命令,最终实现批量下载功能。适合希望自动化获取播客内容的开发者参考。
前言
在闲暇时间,我注意到之前一直关注的TeaHour.fm网站(http://teahour.fm/),一直希望能将其所有音频内容听完。然而转眼两年过去,却未付诸行动。为此,我编写了一个小脚本,抓取了音频的下载链接,计划下载后认真收听。
由于时间紧迫,加上调试过程仅耗时十几分钟,脚本代码可能不够完善,欢迎在评论区提出改进建议。
teahour.py
#!/usr/bin/env python
#coding: utf-8
import sys
import requests
from BeautifulSoup import BeautifulSoup
reload(sys)
sys.setdefaultencoding( "utf-8" )
def parse_index():
l = []
r = requests.get("http://teahour.fm/")
text = r.text
html = BeautifulSoup(text)
a = html.findAll("ul")[-3].findAll("a")
for item in a:
url = "".join(["http://teahour.fm",item.attrs[0][1]])
title = item.text
title = title.strip("#")
l.append((title,url))
return l
def write(lines):
with open("teahour.sh","a") as fh:
fh.writelines(lines)
def parse_sub(t):
title,url = t
r = requests.get(url)
text = r.text
html = BeautifulSoup(text)
audio_url = html.find("audio").attrs[-1][-1]
ext = audio_url.split(".")[-1]
line = "wget '%s' -SO '%s.%s'\n" % (audio_url,title,ext)
#print line
return line
def main():
lines = []
l = parse_index()
for t in l:
line = parse_sub(t)
lines.append(line)
write(lines)
if __name__ == "__main__":
main()
生成的teahour.sh脚本部分内容如下:
peiqiang@budongdeMacBook-Pro:~/Downloads/teahour$ cat teahour.sh wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_89.m4a' -SO '89 和玎玎聊聊如何搭建精益创业团队.m4a' wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_88.m4a' -SO '88 与墨刀创始人张元一聊创业从 0 到 1.m4a' wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_87.m4a' -SO '87 和互联网老兵高春辉聊 IPIP.net.m4a' wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_86.mp3' -SO '86 和『掘金』创始人阴明聊聊创业和技术.mp3' wget 'http://screencasts.b0.upaiyun.com/podcasts/teahour_episode_85.mp3' -SO '85 这次我们聊聊远程工作.mp3'
执行sh teahour.sh命令后,部分下载结果如下:
peiqiang@budongdeMacBook-Pro:~/Downloads/teahour$ ll total 1077896 -rw-r--r-- 1 peiqiang staff 130416640 3 16 20:02 83 这次我们聊聊超酷的Ethereum.m4a -rw-r--r-- 1 peiqiang staff 109631418 3 3 2016 84 继续跟 Robbin 聊聊技术人的战略与战术.m4a -rw-r--r-- 1 peiqiang staff 89519924 6 12 2016 85 这次我们聊聊远程工作.mp3 -rw-r--r-- 1 peiqiang staff 87766246 8 8 2016 86 和『掘金』创始人阴明聊聊创业和技术.mp3 -rw-r--r-- 1 peiqiang staff 59058564 9 11 2016 87 和互联网老兵高春辉聊 IPIP.net.m4a -rw-r--r-- 1 peiqiang staff 32408522 10 26 15:55 88 与墨刀创始人张元一聊创业从 0 到 1.m4a -rw-r--r-- 1 peiqiang staff 43051575 11 8 18:49 89 和玎玎聊聊如何搭建精益创业团队.m4a -rwxr-xr-x@ 1 peiqiang staff 1055 3 16 20:02 teahour.py -rw-r--r-- 1 peiqiang staff 11465 3 16 19:57 teahour.sh
总结
以上就是本文的全部内容,希望能为您的学习或工作提供帮助。如有疑问,欢迎留言交流,感谢支持。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









