怎么用Python写个听小说的爬虫
发表于:2024-09-25 作者:千家信息网编辑
千家信息网最后更新 2024年09月25日,这篇文章主要介绍了怎么用Python写个听小说的爬虫的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用Python写个听小说的爬虫文章都会有所收获,下面我们一起来看看吧
千家信息网最后更新 2024年09月25日怎么用Python写个听小说的爬虫
这篇文章主要介绍了怎么用Python写个听小说的爬虫的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么用Python写个听小说的爬虫文章都会有所收获,下面我们一起来看看吧。
书名和章节列表
随机点开一本书,这个页面可以使用 BeautifulSoup 获取书名和所有单个章节音频的列表。复制浏览器的地址,如:https://www.tingchina.com/yousheng/disp_31086.htm。
from bs4 import BeautifulSoupimport requestsimport reimport randomimport osheaders = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}def get_detail_urls(url): url_list = [] response = requests.get(url, headers=headers) response.encoding = 'gbk' soup = BeautifulSoup(response.text, 'lxml') name = soup.select('.red12')[0].strong.text if not os.path.exists(name): os.makedirs(name) div_list = soup.select('div.list a') for item in div_list: url_list.append({'name': item.string, 'url': 'https://www.tingchina.com/yousheng/{}'.format(item['href'])}) return name, url_list
音频地址
打开单个章节的链接,在 Elements 面板用章节名称作为搜索词,在底部发现了一个 script,这一部分就是声源的地址。
在 Network 面板可以看到,声源的 url 域名和章节列表的域名是不一样的。在获取下载链接的时候需要注意这一点。
def get_mp3_path(url): response = requests.get(url, headers=headers) response.encoding = 'gbk' soup = BeautifulSoup(response.text, 'lxml') script_text = soup.select('script')[-1].string fileUrl_search = re.search('fileUrl= "(.*?)";', script_text, re.S) if fileUrl_search: return 'https://t3344.tingchina.com' + fileUrl_search.group(1)
下载
惊喜总是突如其来,把这个 https://t3344.tingchina.com/xxxx.mp3 放入浏览器中运行居然是 404。
肯定是少了关键性的参数,回到上面 Network 仔细观察 mp3 的 url,发现在 url 后面带了一个 key 的关键字。如下图,这个 key 是来自于 https://img.tingchina.com/play/h6_jsonp.asp?0.5078556568562795 的返回值,可以使用正则表达式将 key 取出来。
def get_key(url): url = 'https://img.tingchina.com/play/h6_jsonp.asp?{}'.format(str(random.random())) headers['referer'] = url response = requests.get(url, headers=headers) matched = re.search('(key=.*?)";', response.text, re.S) if matched: temp = matched.group(1) return temp[len(temp)-42:]
最后的最后在 __main__
中将以上的代码串联起来。
if __name__ == "__main__": url = input("请输入浏览器书页的地址:") dir,url_list = get_detail_urls() for item in url_list: audio_url = get_mp3_path(item['url']) key = get_key(item['url']) audio_url = audio_url + '?key=' + key headers['referer'] = item['url'] r = requests.get(audio_url, headers=headers,stream=True) with open(os.path.join(dir, item['name']),'ab') as f: f.write(r.content) f.flush()
完整代码
from bs4 import BeautifulSoupimport requestsimport reimport randomimport osheaders = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}def get_detail_urls(url): url_list = [] response = requests.get(url, headers=headers) response.encoding = 'gbk' soup = BeautifulSoup(response.text, 'lxml') name = soup.select('.red12')[0].strong.text if not os.path.exists(name): os.makedirs(name) div_list = soup.select('div.list a') for item in div_list: url_list.append({'name': item.string, 'url': 'https://www.tingchina.com/yousheng/{}'.format(item['href'])}) return name, url_list def get_mp3_path(url): response = requests.get(url, headers=headers) response.encoding = 'gbk' soup = BeautifulSoup(response.text, 'lxml') script_text = soup.select('script')[-1].string fileUrl_search = re.search('fileUrl= "(.*?)";', script_text, re.S) if fileUrl_search: return 'https://t3344.tingchina.com' + fileUrl_search.group(1) def get_key(url): url = 'https://img.tingchina.com/play/h6_jsonp.asp?{}'.format(str(random.random())) headers['referer'] = url response = requests.get(url, headers=headers) matched = re.search('(key=.*?)";', response.text, re.S) if matched: temp = matched.group(1) return temp[len(temp)-42:]if __name__ == "__main__": url = input("请输入浏览器书页的地址:") dir,url_list = get_detail_urls() for item in url_list: audio_url = get_mp3_path(item['url']) key = get_key(item['url']) audio_url = audio_url + '?key=' + key headers['referer'] = item['url'] r = requests.get(audio_url, headers=headers,stream=True) with open(os.path.join(dir, item['name']),'ab') as f: f.write(r.content) f.flush()
关于"怎么用Python写个听小说的爬虫"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"怎么用Python写个听小说的爬虫"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。
地址
章节
小说
爬虫
浏览器
浏览
知识
书名
书页
代码
关键
内容
单个
域名
篇文章
链接
面板
音频
声源
输入
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
laravel 服务器
道哥it视频教学网络安全
韩服加好友显示内部服务器错误
网络技术兴起
网络安全法解读
软件开发很难找工作吗
网络技术与科学
代签软件开发合同范本
服务器如何接入视频并识别
北京华信网络技术有限公司
如何快速画数据库关系图
网络技术实时情况
网络安全如何结合业务
建行软件开发中心独立了吗
网络安全工作培训班讲话
公共信息网络安全监察员
网络安全教育简报模板
php数据库成品
龙之谷手游服务器有哪些
大连适合软件开发的公司
数据库双因子认证
陕西电视台网络安全直播
网络安全方面公共预算
wdcp 恢复数据库
天融信网络安全做的怎么样
数据库安全基线工具
企业搭建vpn服务器
软件开发约束是什么
丁柯 网络安全
榆林定制软件开发