Python爬取网站小说保存txt,pdf文件
# 爬取小说 http://www.hengyan.com/dir/9495.aspxfrom lxml.html import etree
import requests
import re
import os
import pdfkithost = "http://www.hengyan.com"
url = "http://www.hengyan.com/dir/9495.aspx"
temp = ''
outPutDirName = 'D:/Book/'
config = pdfkit.configuration(wkhtmltopdf=r"D:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe")if not os.path.exists(outPutDirName):# 如果文件目录不存在则创建目录os.makedirs(outPutDirName)response = requests.get(url)
selector = etree.HTML(response.text)for i in range(1, 43):words = selector.xpath('//*[@id="left"]/div[3]/ul/li[' + str(i) + ']/a/text()')hrefs = selector.xpath('//*[@id="left"]/div[3]/ul/li[' + str(i) + ']/a/@href')address = host + ''.join(hrefs)print(words, address)html = requests.get(address)content = etree.HTML(html.text)block = content.xpath('//*[@class="contentitem"]/div[3]')sub_str = re.sub(u"([^\u4e00-\u9fa5\u0030-\u0039\u0041-\u005a\u0061-\u007a-\u3000])", "", str(''.join(words))) # 剔除非法字符filename = outPutDirName + sub_str + '.txt'for j in block:aaa = j.xpath('./p/text()')bbb = '\n'.join(aaa)bbb.strip()bbb.replace(' ', '')# print(bbb)file = open(filename , 'w')file.write(bbb)file.close()temp = temp + '\n'.join(words) + '</br>' + bbb + '</br>'temp.replace('\n', '</br>')
# 写成pdf
last_pdf = '<html><head><meta charset="UTF-8"></head>' \'<body><div align="center"><p>%s</p></div></body></html>' %temppdfkit.from_string(last_pdf, "./民间烧尸怪谈.pdf", configuration=config)
Python爬取网站小说保存txt,pdf文件相关推荐
- Python爬取起点小说并保存到本地文件夹和MongoDB数据库中
Python爬取起点小说并保存到本地MongoDB数据库中 工具:Python3.7 + Mongo4.0 + Pycharm """ 爬取起点小说<诡秘之主> ...
- Python爬取网站图片并保存,超级简单
Python爬取网站图片并保存,超级简单 先看看结果吧,去bilibili上拿到的图片=-= 第一步,导入模块 import requests from bs4 import BeautifulSou ...
- python爬取网站源代码+图片
python爬取网站源代码+图片 需求分析 基础知识 正则表达式 python网络请求 文件读写 实现 基本思路 具体实现 结果 总结 需求分析 大部分有志青年都想建立属于自己的个人网站,从零开始设计 ...
- 完全小白篇-使用Python爬取网络小说
完全小白篇-使用Python爬取网络小说 一.找一个你要爬取的小说 二.分析网页 网页的展示方式 需要用到的库文件 三.向网站发送请求 四.正则提取 五.跳转的逻辑 六.后续处理 七.保存信息进入do ...
- python爬取顶点小说简单版
python爬取顶点小说简单版 爬取网络资源首先要下载requests库 因为这里面也有数据提取和分析所以也要有etree库,re库 下载库的代码是:pip install 库名 如:pip inst ...
- python爬取有声小说_2019-04-23-Python爬取有声小说
Python爬取有声小说 [toc] 通过python爬取网站的资源,实现批量下载功能: 记录一次自己的学习经历,小白,非专业,难免有不足之处,望读者取其精华! 摘要 功能如下: 1.批量下载 2.批 ...
- Python爬取有声小说
Python爬取有声小说 文章目录 Python爬取有声小说 摘要 1.获取下载链接 2.分析规律,循环爬取 3.保存到本地,批量命名 4.界面设计 5.效果展示 通过python爬取网站的资源,实现 ...
- python爬取网站的图片
python爬取网站的图片 本次爬取图片所需要用到的库:Requests库,BeautifulSoup库,正则表达式,os库. 思路:先爬一张图片,再爬一个网站的图片 先爬一张图片: 首先要得到这张图 ...
- 使用python爬取斗破苍穹小说网
使用python爬取斗破苍穹小说网,学会了以后就不用去看付费的小说了不多bb直接上源码 import requests from bs4 import BeautifulSoupdef file(): ...
最新文章
- 2018html游戏引擎,跨平台三维游戏引擎Unity Pro 2018.1 Win x64
- matlab 仿照案例-目标检测
- 如何向.js文件传变量(如session)
- PowerTip of the Day-Finding Invalid Aliases
- ES6——Symbol
- 支撑位和压力位怎么看是什么意思?
- windows phone 8.0 的网络图片异步加载方案
- highcharts
- 开发、运维、测试都要了解的测试技巧
- c语言转化音乐格式转换器安卓版,音频提取格式转换APP
- Halcon 汉字识别
- mysql转拼音首字母大写_mysql中文字段转拼音首字母,以及中文拼音模糊查询
- 【mysql】In aggregated query without GROUP BY, expression #1 of SELECT list contains ......
- windows11 git 安装SSH密钥
- idea debug报错无法调试 Disconnected from the target VM
- Android开发学习网站集锦
- 拼多多企业入驻条件是什么?和个人入驻有啥区别?
- java jtextfield 居中_如何使JTextField中输入的文字居中显示
- 【周鸿祎】给创业者的三个建议:融…
- 软件项目管理总结(2)软件项目立项和策划