极乐门资源网 Design By www.ioogu.com
本文实例讲述了python实现爬虫抓取小说功能。分享给大家供大家参考,具体如下:
# -*- coding: utf-8 -*- from bs4 import BeautifulSoup from urllib import request import re import os,time #访问url,返回html页面 def get_html(url): req = request.Request(url) req.add_header('User-Agent','Mozilla/5.0') response = request.urlopen(url) html = response.read() return html #从列表页获取小说书名和链接 def get_books(url):#根据列表页,返回此页的{书名:链接}的字典 html = get_html(url) soup = BeautifulSoup(html,'lxml') fixed_html = soup.prettify() books = soup.find_all('div',attrs={'class':'bbox'}) book_dict = {} for book in books: book_name = book.h3.a.string book_url = book.h3.a.get('href') book_dict[book_name] = book_url return book_dict #根据书名链接,获取具体的章节{名称:链接} 的字典 def get_parts(url): html = get_html(url) soup = BeautifulSoup(html,'lxml') fixed_html = soup.prettify() part_urls = soup.find_all('a') host = "http://www.xiaoshuotxt.org" part_dict = {} for p in part_urls: p_url = str(p.get('href')) if re.search(r'\d{5}.html',p_url) and ("xiaoshuotxt" not in p_url): part_dict[p.string] = host + p_url return part_dict #根据章节的url获取具体的章节内容 def get_txt(url): html = get_html(url) soup = BeautifulSoup(html,'lxml') fixed_html = soup.prettify() title = soup.h1.string #获取文章标题 content = soup.find('div',attrs={'class':'zw'}) txt = BeautifulSoup.get_text(content) #正文内容 return txt if __name__ == "__main__": root_dir= r'e:\books' #url = 'http://www.xiaoshuotxt.org/mingzhu/index_2.html' #第2页的小说 url = "http://www.xiaoshuotxt.org/writer/58" #金庸的小说 books = get_books(url) for book_name,book_url in books.items(): os.mkdir(os.path.join(root_dir,book_name)) part_dict = get_parts(book_url) print(book_name,"共:",len(part_dict),"章节") for part_name,part_url in part_dict.items(): print("正在保存:",part_name) f1 = open(r'e:\books\%s\%s.txt'%(book_name,part_name),'w',encoding='utf-8')#以utf-8编码创建文件 part_txt = get_txt(part_url) f1.write(str(part_txt)) f1.close() time.sleep(2)
运行效果:
更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
标签:
python,爬虫,抓取小说
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无python实现爬虫抓取小说功能示例【抓取金庸小说】的评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年09月21日
2024年09月21日
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- 任天堂今晚举行直面会!第三方及独立游戏展示
- 《哆啦A梦的铜锣烧店物语》发售!开罗公式+哆啦A梦
- 任天堂公布《塞尔达传说》系列时间线:野炊与王泪独立在外
- 五条人.2012-一些风景2CD【刀马旦】【WAV+CUE】
- 陈奕迅.2013-Easons.Life演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 许美静.1995-遗憾(新马版)【上华】【WAV+CUE】
- 《叶倩文 歌声情缘》[WAV+CUE][410MB]
- 《张国荣 首首动听经典不容错过 追忆的风 2CD》[WAV+CUE][870MB]
- 《腾格尔 容中尔甲 亚东 高原三星 男人篇 3CD》[WAV/分轨][1GB]
- 命运圣契公测实测可用兑换码大全 命运圣契最新兑换码分享
- 黑神话悟空上品疾蝠精魄获取方法一览|上品疾蝠精魄收集攻略
- 《七龙珠电光炸裂!ZERO》GT角色预告片曝光,15位新角色登场
- [ABC]安娜-胆麦发烧女声[6N纯银镀膜][2016[低速原抓WAV+CUE]
- NewViennaOctetViennaWindSoloists-TheDeccaRecordings(2024)18CD[24-48][FLAC]-7