Python
Python入门系列
Python字典
Python语法
Python代码规范与命名规则
uWSGI和WSGI之间的关系
Python字符编码
Python正则表达式
Python异常处理
Python循环控制
Python文件读写操作
Python模块和函数
Python类
Python格式处理
Python单元测试
Pythonweb采集
python的jQuery-Ajax使用
python的日志
Python变量-字符-数字
Python数组-元祖-字典-集合
Python输入输出
Python安装部署
centos下使用yum安装pip
Python原理说明
Python服务维护
Python模块
Python常用方法
Python新闻
Python使用案例
socket实现客户端和服务端
python射击游戏
将json转换成execl
Python连接Mysql测试
python发送文件
窗口实例化
Python_小说下载脚本
python代码编写规范
本文档使用 MrDoc 发布
-
+
home page
Python_小说下载脚本
小说下载脚本 ```python #!/usr/bin/python3 # -*- coding:UTF-8 -*- from bs4 import BeautifulSoup import requests, sys class downloader(object): def __init__(self): self.server = 'http://www.biqukan.com/' self.target = 'http://www.biqukan.com/1_1094/' self.names = [] #存放章节名 self.urls = [] #存放章节链接 self.nums = 0 #章节数 def get_download_url(self): req = requests.get(url = self.target) html = req.text div_bf = BeautifulSoup(html,"html.parser") div = div_bf.find_all('div', class_ = 'listmain') a_bf = BeautifulSoup(str(div[0]),"html.parser") a = a_bf.find_all('a') self.nums = len(a[15:]) #剔除不必要的章节,并统计章节数 for each in a[15:]: self.names.append(each.string) self.urls.append(self.server + each.get('href')) def get_contents(self, target): req = requests.get(url = target) html = req.text bf = BeautifulSoup(html) texts = bf.find_all('div', class_ = 'showtxt') texts = texts[0].text.replace('\xa0'*8,'\n\n') return texts """ 函数说明:将爬取的文章内容写入文件 Parameters: name - 章节名称(string) path - 当前路径下,小说保存名称(string) text - 章节内容(string) Returns: 无 Modify: 2017-09-13 """ def writer(self, name, path, text): write_flag = True with open(path, 'a', encoding='utf-8') as f: f.write(name + '\n') f.writelines(text) f.write('\n\n') if __name__ == "__main__": dl = downloader() dl.get_download_url() print('《一年永恒》开始下载:') for i in range(dl.nums): dl.writer(dl.names[i], '一念永恒.txt', dl.get_contents(dl.urls[i])) sys.stdout.write(" 已下载:%.3f%%" % float(i/dl.nums) + '\r') sys.stdout.flush() print('《一年永恒》下载完成') ``` 输出目录标题,主要是字符编码转换 ```python #!/usr/bin/python3 # *-* coding:utf-8 *-* from bs4 import BeautifulSoup import requests if __name__ == "__main__": server = 'http://www.biqukan.com/' target = 'http://www.biqukan.com/1_1094/' req = requests.get(url = target) req.encoding="gbk" #换字符编码 html = req.text div_bf = BeautifulSoup(html,"html.parser") div = div_bf.find_all('div', class_ = 'listmain') a_bf = BeautifulSoup(str(div[0]),"html.parser") a = a_bf.find_all('a') for each in a: print(each.string, server + each.get('href')) ```
日行一善
April 23, 2021, 8:42 a.m.
Share documents
Collection documents
Last
Next
Scan wechat
Copy link
Scan your mobile phone to share
Copy link
关于 MrDoc
觅思文档MrDoc
是
州的先生
开发并开源的在线文档系统,其适合作为个人和小型团队的云笔记、文档和知识库管理工具。
如果觅思文档给你或你的团队带来了帮助,欢迎对作者进行一些打赏捐助,这将有力支持作者持续投入精力更新和维护觅思文档,感谢你的捐助!
>>>捐助鸣谢列表
微信
支付宝
QQ
PayPal
QQ粉丝交流群:882382311
Markdown文件
share
link
type
password
Update password