# 爬取小说 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文件相关推荐

  1. Python爬取起点小说并保存到本地文件夹和MongoDB数据库中

    Python爬取起点小说并保存到本地MongoDB数据库中 工具:Python3.7 + Mongo4.0 + Pycharm """ 爬取起点小说<诡秘之主> ...

  2. Python爬取网站图片并保存,超级简单

    Python爬取网站图片并保存,超级简单 先看看结果吧,去bilibili上拿到的图片=-= 第一步,导入模块 import requests from bs4 import BeautifulSou ...

  3. python爬取网站源代码+图片

    python爬取网站源代码+图片 需求分析 基础知识 正则表达式 python网络请求 文件读写 实现 基本思路 具体实现 结果 总结 需求分析 大部分有志青年都想建立属于自己的个人网站,从零开始设计 ...

  4. 完全小白篇-使用Python爬取网络小说

    完全小白篇-使用Python爬取网络小说 一.找一个你要爬取的小说 二.分析网页 网页的展示方式 需要用到的库文件 三.向网站发送请求 四.正则提取 五.跳转的逻辑 六.后续处理 七.保存信息进入do ...

  5. python爬取顶点小说简单版

    python爬取顶点小说简单版 爬取网络资源首先要下载requests库 因为这里面也有数据提取和分析所以也要有etree库,re库 下载库的代码是:pip install 库名 如:pip inst ...

  6. python爬取有声小说_2019-04-23-Python爬取有声小说

    Python爬取有声小说 [toc] 通过python爬取网站的资源,实现批量下载功能: 记录一次自己的学习经历,小白,非专业,难免有不足之处,望读者取其精华! 摘要 功能如下: 1.批量下载 2.批 ...

  7. Python爬取有声小说

    Python爬取有声小说 文章目录 Python爬取有声小说 摘要 1.获取下载链接 2.分析规律,循环爬取 3.保存到本地,批量命名 4.界面设计 5.效果展示 通过python爬取网站的资源,实现 ...

  8. python爬取网站的图片

    python爬取网站的图片 本次爬取图片所需要用到的库:Requests库,BeautifulSoup库,正则表达式,os库. 思路:先爬一张图片,再爬一个网站的图片 先爬一张图片: 首先要得到这张图 ...

  9. 使用python爬取斗破苍穹小说网

    使用python爬取斗破苍穹小说网,学会了以后就不用去看付费的小说了不多bb直接上源码 import requests from bs4 import BeautifulSoupdef file(): ...

最新文章

  1. 2018html游戏引擎,跨平台三维游戏引擎Unity Pro 2018.1 Win x64
  2. matlab 仿照案例-目标检测
  3. 如何向.js文件传变量(如session)
  4. PowerTip of the Day-Finding Invalid Aliases
  5. ES6——Symbol
  6. 支撑位和压力位怎么看是什么意思?
  7. windows phone 8.0 的网络图片异步加载方案
  8. highcharts
  9. 开发、运维、测试都要了解的测试技巧
  10. c语言转化音乐格式转换器安卓版,音频提取格式转换APP
  11. Halcon 汉字识别
  12. mysql转拼音首字母大写_mysql中文字段转拼音首字母,以及中文拼音模糊查询
  13. 【mysql】In aggregated query without GROUP BY, expression #1 of SELECT list contains ......
  14. windows11 git 安装SSH密钥
  15. idea debug报错无法调试 Disconnected from the target VM
  16. Android开发学习网站集锦
  17. 拼多多企业入驻条件是什么?和个人入驻有啥区别?
  18. java jtextfield 居中_如何使JTextField中输入的文字居中显示
  19. 【周鸿祎】给创业者的三个建议:融…
  20. 软件项目管理总结(2)软件项目立项和策划

热门文章

  1. HTML清新卡通人物404单页源码分享
  2. l351墨水灯和缺纸灯闪烁_爱普生L351有墨水,可缺墨的灯一直闪,怎么解决?
  3. CEC 2021基准测试集
  4. java pdfbox maven_java pdfbox2.0.0 获取表格和字
  5. 华为光伏逆变器无线数据采集接线说明
  6. Android音频系统的改进设想和展望 PulseAudio介绍
  7. 抛物样条曲线(最详细简单的解析)
  8. mybatisplus解除分页限制
  9. .NET技术在中国为什么老被人嫌弃
  10. Android 项目更换头像(拍照和选择相册)