Python多线程如何下载有声小说
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇文章跟大家分析一下"Python多线程如何下载有声小说"。内容详细易懂,对"Python多线程如何下载有声小说"感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面
千家信息网最后更新 2025年11月11日Python多线程如何下载有声小说
这篇文章跟大家分析一下"Python多线程如何下载有声小说"。内容详细易懂,对"Python多线程如何下载有声小说"感兴趣的朋友可以跟着小编的思路慢慢深入来阅读一下,希望阅读后能够对大家有所帮助。下面跟着小编一起深入学习"Python多线程如何下载有声小说"的知识吧。
我特地买了大屏幕的Note II 以便看pdf,另外耳朵也不能闲着,不过咱不是听英语而是听小说,我在读书的时候就喜欢听广播,特别是说书、相声等,所以我需要大量的有声小说,现在网上这些资源多的很,但是下载页记为麻烦,为了挣取更多的流量和广告点击,这些网站的下载链接都需要打开至少两个以上的网页才能找到真正的链接,甚是麻烦,为了节省整体下载时间,我写了这个小程序,方便自己和大家下载有声小说(当然,还有任何其他类型的资源)
先说明一下,我不是为了爬很多资料和数据,仅仅是为了娱乐和学习,所以这里不会漫无目的的取爬取一个网站的所有链接,而是给定一个小说,比方说我要下载小说《童年》,我会在我听评书网上找到该小说的主页然后用程序下载所有mp3音频,具体做法见下面代码,所有代码都在模块crawler5tps中:
1. 先设定一下start url 和保存文件的目录
#-*-coding:GBK-*- import urllib,urllib2 import re,threading,os baseurl = 'http://www.5tps.com' #base url down2path = 'E:/enovel/' #saving path save2path = '' #saving file name (full path)
2. 从start url 解析下载页面的url
def parseUrl(starturl): ''''' parse out download page from start url. eg. we can get 'http://www.5tps.com/down/8297_52_1_1.html' from 'http://www.5tps.com/html/8297.html' ''' global save2path rDownloadUrl = re.compile(".*?.{4}\s{1}(.*)\s{1}.*") #有声小说 闷骚1 播音:刘涛 全集 f = urllib2.urlopen(starturl) totalLine = f.readlines() ''''' create the name of saving file ''' title = totalLine[3].split(" ")[1] if os.path.exists(down2path+title) is not True: os.mkdir(down2path+title) save2path = down2path+title+"/" downUrlLine = [ line for line in totalLine if rDownloadUrl.match(line)] downLoadUrl = []; for dl in downUrlLine: while True: m = rDownloadUrl.match(dl) if not m: break downUrl = m.group(1) downLoadUrl.append(downUrl.strip()) dl = dl.replace(downUrl,'') return downLoadUrl3. 从下载页面解析出真正的下载链接
def getDownlaodLink(starturl): ''''' find out the real download link from download page. eg. we can get the download link 'http://180j-d.ysts8.com:8000/人物纪实/童年/001.mp3?\ 1251746750178x1356330062x1251747362932-3492f04cf54428055a110a176297d95a' from \ 'http://www.5tps.com/down/8297_52_1_1.html' ''' downUrl = [] gbk_ClickWord = '点此下载' downloadUrl = parseUrl(starturl) rDownUrl = re.compile(''+gbk_ClickWord+'.*') #find the real download link for url in downloadUrl: realurl = baseurl+url print realurl for line in urllib2.urlopen(realurl).readlines(): m = rDownUrl.match(line) if m: downUrl.append(m.group(1)) return downUrl4. 定义下载函数
def download(url,filename): ''''' download mp3 file ''' print url urllib.urlretrieve(url, filename)
5. 创建用于下载文件的线程类
class DownloadThread(threading.Thread): ''''' dowanload thread class ''' def __init__(self,func,savePath): threading.Thread.__init__(self) self.function = func self.savePath = savePath def run(self): download(self.function,self.savePath)
6. 开始下载
if __name__ == '__main__': starturl = 'http://www.5tps.com/html/8297.html' downUrl = getDownlaodLink(starturl) aliveThreadDict = {} # alive thread downloadingUrlDict = {} # downloading link i = 0; while i < len(downUrl): ''''' Note:我听评说网 只允许同时有三个线程下载同一部小说,但是有时受网络等影响,\ 为确保下载的是真实的mp3,这里将线程数设为2 ''' while len(downloadingUrlDict)< 2 : downloadingUrlDict[i]=i i += 1 for urlIndex in downloadingUrlDict.values(): #argsTuple = (downUrl[urlIndex],save2path+str(urlIndex+1)+'.mp3') if urlIndex not in aliveThreadDict.values(): t = DownloadThread(downUrl[urlIndex],save2path+str(urlIndex+1)+'.mp3') t.start() aliveThreadDict[t]=urlIndex for (th,urlIndex) in aliveThreadDict.items(): if th.isAlive() is not True: del aliveThreadDict[th] # delete the thread slot del downloadingUrlDict[urlIndex] # delete the url from url list needed to download print 'Completed Download Work'这样就可以了,让他尽情的下吧,咱还得码其他的项目去,哎 >>>
关于Python多线程如何下载有声小说就分享到这里啦,希望上述内容能够让大家有所提升。如果想要学习更多知识,请大家多多留意小编的更新。谢谢大家关注一下网站!
小说
线程
链接
网站
学习
代码
内容
文件
更多
知识
程序
而是
资源
跟着
页面
麻烦
三个
两个
人物
做法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
nsa数据库
惠普服务器ssd 写入量
东莞手机软件开发收费
网络技术专科好找工作吗
繁字体软件开发
车速拍网络技术招聘
英雄联盟钻石才能进的服务器名字
java服务器要钱吗
惠普1200w服务器电源针脚
魅族的推送服务器
ps安装失败安装程序数据库
海云安网络安全技术
河南大学软件开发专业
生产服务器密码管理流程
安装的数据库怎么没有bin
修改win10 网络安全设置
玉米基因数据库使用
域名绑服务器
cf找不到服务器里的角色
电子网络技术学校招生
车载网络技术教材
深圳市华融网络技术有限公司
电脑自定义功能软件开发
2018网络技术考点
广州市网络安全周2021
c 软件开发主要做什么的
华为网络技术工程师待遇
义乌网络安全吗
彩票软件开发月薪多少
网络安全三不伤害