继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说

# -*- coding: utf- -*-
import urllib.request
import bs4
import re # 爬取源码
def getHtml(url):
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
headers = {"User-Agent":user_agent}
request = urllib.request.Request(url,headers=headers)
response = urllib.request.urlopen(request)
html = response.read()
return html # 爬取整个网页
def parse(url):
html_doc = getHtml(url)
sp = bs4.BeautifulSoup(html_doc, 'html.parser', from_encoding="utf-8")
return sp # 获取书籍目录
def get_book_dir(url):
books_dir = []
name = parse(url).find('div', class_='listmain')
if name:
dd_items = name.find('dl')
dt_num =
for n in dd_items.children:
ename = str(n.name).strip()
if ename == 'dt':
dt_num +=
if ename != 'dd':
continue
books_info = {}
        if dt_num == 2:
                durls = n.find_all('a')[0]
                books_info['name'] = (durls.get_text())
                books_info['url'] = 'http://www.biqukan.com' + durls.get('href')
                books_dir.append(books_info)
    return books_dir # 获取章节内容
def get_charpter_text(curl):
    text = parse(curl).find('div', class_='showtxt')
    if text:
        cont = text.get_text()
        cont = [str(cont).strip().replace('\r \xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0', '').replace('\u3000\u3000', '')]
        c = " ".join(cont)
        ctext = re.findall(r'^.*?html', c)
        return ctext
    else:
        return '' # 获取书籍
def get_book(burl):
    # 目录
    book = get_book_dir(burl)
    if not book:
        return book     # 内容
    for d in book:
        curl = d['url']
        try:
            print('正在获取章节【{}】【内容】【{}】'.format(d['name'],d['url']))
            ctext = get_charpter_text(curl)
            d['text'] = ctext
            print(d['text'])
            print

首先需要模拟浏览器访问url,爬取源码,然后进行分析(目录,小说内容),然后据此获取想要爬取的内容

python之爬取小说的更多相关文章

  1. python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)

    转载出处:药少敏   ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...

  2. Python实战项目网络爬虫 之 爬取小说吧小说正文

    本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...

  3. Python爬虫爬取全书网小说,程序源码+程序详细分析

    Python爬虫爬取全书网小说教程 第一步:打开谷歌浏览器,搜索全书网,然后再点击你想下载的小说,进入图一页面后点击F12选择Network,如果没有内容按F5刷新一下 点击Network之后出现如下 ...

  4. 用Python爬取小说《一念永恒》

    我们首先选定从笔趣看网站爬取这本小说. 然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了. 那么下面,我们直接来看代码. ...

  5. Python爬虫|爬取喜马拉雅音频

    "GOOD Python爬虫|爬取喜马拉雅音频 喜马拉雅是知名的专业的音频分享平台,用户规模突破4.8亿,汇集了有声小说,有声读物,儿童睡前故事,相声小品等数亿条音频,成为国内发展最快.规模 ...

  6. Scrapy爬取小说简单逻辑

    Scrapy爬取小说简单逻辑 一 准备工作 1)安装Python 2)安装PIP 3)安装scrapy 4)安装pywin32 5)安装VCForPython27.exe ........... 具体 ...

  7. python3下BeautifulSoup练习一(爬取小说)

    上次写博客还是两个月以前的事,今天闲来无事,决定把以前刚接触python爬虫时的一个想法付诸行动:就是从网站上爬取小说,这样可以省下好多流量(^_^). 因为只是闲暇之余写的,还望各位看官海涵:不足之 ...

  8. Python3爬取小说并保存到文件

    问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...

  9. 大神:python怎么爬取js的页面

    大神:python怎么爬取js的页面 可以试试抓包看看它请求了哪些东西, 很多时候可以绕过网页直接请求后面的API 实在不行就上 selenium (selenium大法好) selenium和pha ...

随机推荐

  1. vue 事件结合双向数据绑定实现todolist

    <template> <div id="app"> <input type="text" v-model='todo' /> ...

  2. dtypes.py", line 499 _np_qint8 = np.dtype([("qint8", np.int8, (1,)])

    Traceback (most recent call last): File "<stdin>", line 1, in <module> File &q ...

  3. EXCEL导入配置开发

    1.登录infor 企业级 选择 配置 EXCEL导入配置 2.新增配置(设置模板名.说明.存储过程名称.应用仓库) 后保存 3.将excel模板放到服务器 路径:/opt/infor/sce/wil ...

  4. Laya的动画制作,整图动画,序列图动画,时间轴动画,龙骨动画

    参考: Laya动画基础 Egret帧动画工具类 版本:2.1.1.1 序列图动画 白鹭的序列图动画,使用TexureMerger合图,然后使用MovieClip类播放. Laya的话,使用图集打包工 ...

  5. 【443】Tweets Analysis Q&A

        [Question 01] When converting Tweets info to csv file, commas in the middle of data (i.e. locati ...

  6. 123457123457#0#-----com.tym.YuErBaiKeTYM--前拼后广--育儿百科

    com.tym.YuErBaiKeTYM--前拼后广--育儿百科

  7. 多个wav音频文件合并(连接)成一个文件

    场景:一段声音从浏览器麦克风缓冲上一段一段发给服务器,按照时间戳生成很多文件. 目的:把他们按时间顺序连到一个时间轴上. 命令如下: ffmpeg -f concat -i list.txt out. ...

  8. windows7 + iis7 + fastcgi + php5 + netbeans + xdebug 调试 php

    Xdebug是一个开放源代码的PHP程序调试器(即一个Debug工具),可以用来跟踪,调试和分析PHP程序的运行状况. windows7 + iis7 + fastcgi + php5 + netbe ...

  9. 【Leetcode_easy】949. Largest Time for Given Digits

    problem 949. Largest Time for Given Digits solution: class Solution { public: string largestTimeFrom ...

  10. e.target 和 e.currentTarget

    们可以得出: e.currentTarget指的是注册了事件监听器的对象,而e.target指的是该对象里的子对象,也是触发这个事件的对象!这么说应该明白 了吧?