python 爬qidian小说
import re
import urllib.request
from bs4 import BeautifulSoup
import time url=input("第一章网址:") def gethtml(url):
#获取页面源代码html
page=urllib.request.urlopen(url)
html=page.read().decode('utf-8') #html是一个列表
soup=BeautifulSoup(html,'html.parser') return soup def getcontent(soup,load): content=soup.find_all("div",{"class":"read-content j_readContent"}) content1=re.compile(r'<p>([\s\S]*?)</p>') #匹配到段落内容 content2=content1.findall(str(content)) content3=re.sub("</?\w+[^>]*>",'',content2[0]) #除掉html标签 content4=content3.replace('。','。\n\n\0\0\0') #把以句号换位“。\n\n\0\0\0 两个换行符三个空格” 到此,将章节内容获取完毕 contentname=re.compile(r'<h3 class="j_chapterName">(.*?)</h3>') contentname1=contentname.findall(str(soup)) #获取章节名称 book="----------------------------------------------------------------"+contentname1[0]+"------------------------------------------------------------\n\n\n"+content4 with open(load, 'a') as f: f.write(book) def nextcontent(soup): content=soup.find_all("div",{"class":"chapter-control dib-wrap"}) #print(str(content)) step=re.compile(r'<a data-eid="qd_R109" href="(.*?)" id="j_chapterNext">') content1=step.findall(str(content)) if content1 == []: #判断该页是否为最后一章,是,获取最后一章(特殊)的url,不是,以常规方法获取下一章url step1=re.compile(r'<a data-eid="qd_R118" href="(.*?)" id="j_chapterNext">') content2=step1.findall(str(content)) url="http:"+content2[0] return url
else:
url="http:"+content1[0] return url def panduan(soup): content=soup.find_all("div",{"class":"chapter-control dib-wrap"}) #print(str(content)) step=re.compile(r'<a data-eid="qd_R109" href="(.*?)" id="j_chapterNext">') content1=step.findall(str(content)) return content1
#------------------------------------------------------------------------- #------------------------------------------------------------------------- soup=gethtml(url)
bookname=re.findall(r'<h1>(.*?)</h1>' ,str(soup)) #匹配书名 load="d:/88/%s.txt" % bookname[0]
i=0
while 1==1:
soup=gethtml(url)
getcontent(soup,load)
url=nextcontent(soup)
content1=panduan(soup) #在该章里匹配下一章的url,若无法匹配到(输出为[]空),说明没有下一章
i+=1
print("第%d章下载完成" % i) if content1 == []: #
break time.sleep(0.2)
下一篇,将结合该篇写一个爬取某一页所有小说的爬虫
(本文仅供技术参考,请勿用作非法途径)
python 爬qidian小说的更多相关文章
- 用Python爬取小说《一念永恒》
我们首先选定从笔趣看网站爬取这本小说. 然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了. 那么下面,我们直接来看代码. ...
- python爬取小说详解(一)
整理思路: 首先观察我们要爬取的页面信息.如下: 自此我们获得信息有如下: ♦1.小说名称链接小说内容的一个url,url的形式是:http://www.365haoshu.com/Book/Cha ...
- 详细记录了python爬取小说《元尊》的整个过程,看了你必会~
学了好几天的渗透测试基础理论,周末了让自己放松一下,最近听说天蚕土豆有一本新小说,叫做<元尊>,学生时代的我可是十分喜欢读天蚕土豆的小说,<斗破苍穹>相信很多小伙伴都看过吧.今 ...
- 用python爬取小说章节内容
在学爬虫之前, 最好有一些html基础, 才能更好的分析网页. 主要是五步: 1. 获取链接 2. 正则匹配 3. 获取内容 4. 处理内容 5. 写入文件 代码如下: #导入相关model fro ...
- python爬取小说
运行结果: 代码: import requests from bs4 import BeautifulSoup from selenium import webdriver import os cla ...
- python入门学习之Python爬取最新笔趣阁小说
Python爬取新笔趣阁小说,并保存到TXT文件中 我写的这篇文章,是利用Python爬取小说编写的程序,这是我学习Python爬虫当中自己独立写的第一个程序,中途也遇到了一些困难,但是最后 ...
- Python3爬取小说并保存到文件
问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...
- Python实战项目网络爬虫 之 爬取小说吧小说正文
本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...
- python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)
转载出处:药少敏 ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...
随机推荐
- [security][modsecurity] modsecurity 规则说明/中文/转发
原文转发以防丢失. 地址: http://www.catssec.com:8090/exploit/?p=691 转来细读之后,并没有太多的参考价值 :( modsecurity规则手册 通用格式 ...
- Flink - ShipStrategyType
对于DataStream,可以选择如下的Strategy, /** * Sets the partitioning of the {@link DataStream} so that the ou ...
- Copycat - Overview
Copycat’s primary role is as a framework for building highly consistent, fault-tolerant replicated s ...
- 【pyqtgraph绘图】案例-动态的正余弦波形图
先看一个简单的小例子: 完整代码: import numpy as np import pyqtgraph as pg import sys from PyQt5.QtWidgets import Q ...
- pyinstaller-打包python程序为exe文件
pyinstaller ---转载文章 视频:https://www.bilibili.com/video/av21670971/ PyInstaller可以用来打包python应用程序,打包完的程序 ...
- Ubuntu14.04 LTS 安装Chrome浏览器(转)
add zhj: 亲测过,可以,原来不用FQ就可以下载,有点意外 原文:http://www.jianshu.com/p/8220578d0b15 1.打开终端(ctrl + alt + t),下载6 ...
- Linux中安装python3
[centos7中安装python3]http://blog.csdn.net/wjqwinn/article/details/75633714 (一)安装python3前的准备工作1.修改文件中第一 ...
- 016-插件使用-head
一.安装以及概览 elasticsearch-head将是一款专门针对于elasticsearch的客户端工具 elasticsearch-head配置包,下载地址:https://github.co ...
- 008-Centos 7.x安装 Ambari 2.2.2 + HDP 2.4.2 搭建Hadoop集群
1.安装环境说明 安装前先安装好 Centos 7.2, jdk-8u91, mysql5.7.13 一共有3台机器,一个是主节点192.168.111.10,两个是从:192.168.111.11, ...
- 802.11n 连接的建议设置是什么?
这些是用于支持 802.11N 的英特尔无线适配器的默认设置. 这些建议采用的设置可以在英特尔® PROSet/ 无线软件的 高级菜单上找到. 属性 值 频带 2.4 的 802.11n 通道宽度 自 ...