用Python爬取小说《一念永恒》
我们首先选定从笔趣看网站爬取这本小说。
然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了。

那么下面,我们直接来看代码。
from urllib import request
from bs4 import BeautifulSoup
import re
import sys if __name__ == "__main__":
#创建txt文件
file = open('一念永恒.txt', 'w', encoding='utf-8')
#一念永恒小说目录地址
target_url = 'http://www.biqukan.com/1_1094/'
head = {}
head['User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19'
target_req = request.Request(url = target_url, headers = head)
target_response = request.urlopen(target_req)
target_html = target_response.read().decode('gbk','ignore')
listmain_soup = BeautifulSoup(target_html)
#找出div标签中class为listmain的所有子标签
chapters = listmain_soup.find_all('div',class_ = 'listmain')
download_soup = BeautifulSoup(str(chapters))
#计算章节个数
numbers = (len(download_soup.dl.contents) - 1) / 2 - 8
index = 1
begin_flag = False
for child in download_soup.dl.children:
if child != '\n':
#找到《一念永恒》正文卷
if child.string == u"《一念永恒》正文卷":
begin_flag = True
#爬取链接并下载链接内容
if begin_flag == True and child.a != None:
download_url = "http://www.biqukan.com" + child.a.get('href')
download_req = request.Request(url = download_url, headers = head)
download_response = request.urlopen(download_req)
download_html = download_response.read().decode('gbk','ignore')
download_name = child.string
soup_texts = BeautifulSoup(download_html)
texts = soup_texts.find_all(id = 'content', class_ = 'showtxt')
soup_text = BeautifulSoup(str(texts))
write_flag = True
file.write(download_name + '\n\n')
#将爬取内容写入文件
for each in soup_text.div.text.replace('\xa0',''): if each == 'h':
write_flag = False
if write_flag == True and each != ' ':
file.write(each)
if write_flag == True and each == '\r':
file.write('\n')
print('正在写入第{0}小节'.format(index))
index+=1
file.write('\n\n')
#打印爬取进度
sys.stdout.write("已下载:%.3f%%" % float(index/numbers) + '\r')
sys.stdout.flush()
index += 1
file.close()
这个代码可能还存在着一些小问题,但是并不影响我们爬取小说,下面来看看我们的运行结果。


用Python爬取小说《一念永恒》的更多相关文章
- 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之爬取小说
继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...
- python入门学习之Python爬取最新笔趣阁小说
Python爬取新笔趣阁小说,并保存到TXT文件中 我写的这篇文章,是利用Python爬取小说编写的程序,这是我学习Python爬虫当中自己独立写的第一个程序,中途也遇到了一些困难,但是最后 ...
- Python3爬取小说并保存到文件
问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...
- Python实战项目网络爬虫 之 爬取小说吧小说正文
本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...
- python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)
转载出处:药少敏 ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...
随机推荐
- 【MySQL】Mac通过brew安装的MySQL重启方法
问题 在 Mac 上通过 brew install mysql 安装的的MySQL使用基本MySQL命令重启会失败: mysql.server stop mysql.server start mysq ...
- Dart函数方法
/* 内置方法/函数: print(); 自定义方法: 自定义方法的基本格式: 返回类型 方法名称(参数1,参数2,...){ 方法体 return 返回值; } */ void printInfo( ...
- Python的collections之namedtuple的使用及其优势
类实现: class User: def __init__(self, name, age, height): self.name = name self.age = age self.height ...
- ISO/IEC 9899:2011 条款6.5.1——基本表达式
6.5.1 基本表达式 语法 1.primary-expression: identifier constant string-literal ( expression ) generic ...
- 【kubernetes secret 和 aws ecr helper】kubernetes从docker拉取image,kubernetes docker私服认证(argo docker私服认证),no basic auth credentials错误解决
aws ecr helper: https://aws.amazon.com/blogs/compute/authenticating-amazon-ecr-repositories-for-dock ...
- [转]如何更换 Ubuntu 18.04 LTS 的 GDM 登录界面背景
链接地址:https://www.linuxprobe.com/ubuntu-gdm-login.html
- vi 替换命令 以及“找不到模式”解决
转自:https://www.cnblogs.com/zfyouxi/p/5181363.html 在linux vi编辑工具中使用替换命令操作时,会出现明明有匹配查找模式的数据.却报“找不到模式”问 ...
- PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'exchange' in vhost '/': received 'direct
最近在搞rabbitmq,把解决问题方法记下 问题1:PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'exchange' in ...
- python3多线程的运用
Python3线程 很大一堆数据需要处理,加速效率使用多线程可以节省运算的时间. 多线程基础 threading.active_count() 目前多少个激活的线程 threading.enumera ...
- ipad 如何 Airplay 到 Windows 上?
最近刚好有用到这个,觉得还不错,就推荐给大家.虽然个人并不喜欢苹果这一套,但是工作里难免掺杂一些会用到的ipad , mac . 纯粹技术分享. 1. 5KPlayer : https://www.5 ...