免app下载笔趣阁小说

  这个是对最近学习的一次总结吧。前两天写的,今天才有时间写博客。

  偶然点开笔趣阁的网址(https://www.biquge.cc/),突然觉得我应该可以用爬虫实现小说下载。有这个想法我就开始尝试了。

  

  爬虫呀,说白了就是程序自动模拟浏览器操作来获取网页的内容。

  先用F12查看元素,查看章节网址链接,和章节正文内容。

  结构很简单。

  想法很快就有了,通过网站的搜索打开小说详情页,然后获取每一章的网址url,依次访问每一章网址,再通过正则表达式匹配章节内容,

最后将匹配的内容保存到本地。

  中间忘了一个小的知识点,就是我使用re.findall()来匹配的,它最后返回的时一个列表!!!

  运行结果如下图:

  

  

  代码如下:

  

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/20 15:46
# @Author  : yuantup
# @Site    :
# @File    : biquge.py
# @Software: PyCharm
import urllib.request
import re
import time
import os

def open_url(url):
    # 打开网址专用
    # 以字典的形式设置headers
    head = {‘Accept‘: ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8‘,
            # ‘Accept-Encoding‘: ‘gzip‘,
            # 接受编码如果是gzip,deflate之类的,可能会报错
            ‘Accept-Language‘: ‘zh-CN,zh;q=0.9‘,
            ‘Connection‘: ‘keep-alive‘,
            ‘Host‘: ‘sou.xanbhx.com‘,
            ‘Referer‘: ‘https://www.biquge.cc/‘,
            ‘Upgrade-Insecure-Requests‘: ‘1‘,
            ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) ‘
                          ‘Chrome/63.0.3239.132 Safari/537.36‘, }
    # 设置cookies
    # proxy = urllib.request.ProxyHandler({‘http‘: ‘127.0.0.1:8888‘})
    opener = urllib.request.build_opener()
    # 遍历字典,将其转换为指定格式(外层列表,里层元组)
    headers = []
    for key, value in head.items():
        item = (key, value)
        headers.append(item)
    opener.addheaders = headers
    urllib.request.install_opener(opener)
    response = urllib.request.urlopen(url)
    html = response.read()
    time.sleep(1)
    return html

def novel_detail(book_name):
    # 根据传入的小说名字获取到小说的详情页,并提取出小说内容(详情,每个章节的网址)
    # 小说存在重名情况!!!待解决
    zh_book_name = urllib.request.quote(book_name)
    url = ‘https://sou.xanbhx.com/search?siteid=biqugecc&q=‘ + zh_book_name
    html = open_url(url).decode(‘utf-8‘)
    # print(html)
    name_pa = ‘<span class="s2">.*?<a href="(.*?)" target="_blank">.*?(\S*?)</a>‘
    name_list = re.findall(name_pa, html, re.S)
    # print(name_list[1])
    if name_list[0][1] == book_name:
        book_url = name_list[0][0]
        print(book_url)
    elif not name_list:
        print(‘‘)
        print(‘对不起,该网址没有找到你需要的书。‘)
    return book_url

def content(url):
    # 获取小说正文
    html = open_url(url).decode(‘utf-8‘)
    # print(html)
    main_body_pa = r‘最新章节(提示:已启用缓存技术,最新章节可能会延时显示,登录书架即可实时查看。).*?<dt>(.*?)</div>‘
    chapter_url_pa = r‘<a style="" href="(.*?)">‘
    main_body = re.findall(main_body_pa, html, re.S)
    # print(main_body, ‘ 1‘)
    # 记住re.findall()方法返回的时一个列表!!!
    chapter_url = re.findall(chapter_url_pa, main_body[0])
    # print(chapter_url, ‘ 2‘)
    time.sleep(2)
    return chapter_url

def save_novel(novel_url, content_url_list, book_name):
    # 保存小说内容
    for i in range(len(content_url_list)):
        real_url = novel_url + content_url_list[i]
        html = open_url(real_url).decode(‘utf-8‘)
        # print(html)
        chapter_name_pa = ‘<h1>(.*?)</h1>‘
        chapter_name = re.search(chapter_name_pa, html).group(1)
        # print(chapter_name)
        # print(type(chapter_name))
        content_pa = r‘<div id="content">(.*?)<script>‘
        content1 = re.findall(content_pa, html, re.S)
        content2 = content1[0].replace(‘&nbsp;&nbsp;&nbsp;&nbsp;‘, ‘ ‘)
        content3 = content2.replace(‘<br/>‘, ‘\n‘)
        content4 = content3.replace(‘</br>‘, ‘‘)
        re_chapter_name = chapter_name.replace(‘ ‘, ‘‘)
        content5 = content4.replace(re_chapter_name, ‘‘)
        # 有些章节内容包括章节名,这里替换掉它们。
        whole_content = ‘   ‘ + chapter_name + ‘\n‘ + content5
        # print(whole_content)
        # print(chapter_name)
        with open(book_name + ‘.txt‘, ‘a‘, encoding=‘utf-8‘) as f:
            f.write(whole_content)
            print(‘成功下载  {}‘.format(chapter_name))
        time.sleep(1)

def main():
    path = r‘E:\spiser_sons\books‘
    a = os.getcwd()
    print(a)
    if os.path.exists(path):
        os.chdir(path)
        print(os.getcwd())
    else:
        os.mkdir(path)
        os.chdir(path)
    book_name = input(‘请输入想下载小说的名字:‘)
    novel_url = novel_detail(book_name)
    content_url_list = content(novel_url)
    save_novel(novel_url, content_url_list, book_name)

if __name__ == ‘__main__‘:
    main()

原文地址:https://www.cnblogs.com/yuantup/p/9842337.html

时间: 2024-08-30 14:21:15

免app下载笔趣阁小说的相关文章

python入门学习之Python爬取最新笔趣阁小说

Python爬取新笔趣阁小说,并保存到TXT文件中      我写的这篇文章,是利用Python爬取小说编写的程序,这是我学习Python爬虫当中自己独立写的第一个程序,中途也遇到了一些困难,但是最后迎刃而解了.这个程序非常的简单,程序的大概就是先获取网页的源代码,然后在网页的源代码中提取每个章节的url,获取之后,在通过每个url去获取文章的内容,在进行提取内容,然后就是保存到本地,一TXT的文件类型保存.大概是这样1:获取网页源代码2:获取每章的url3:获取每章的内容4:下载保存文件中 1

Python 爬取笔趣阁小说

最近在学习 Python,觉得爬虫很好玩,今天我准备爬取我看了至少三遍的小说<雪中悍刀行>,作者是烽火戏诸侯,他的小说很有才华,有着很多的粉丝,但他很多部小说都处于断更状态,因此人称大内总管. 我准备爬取小说的网站是新笔趣阁,这里一个盗版网站,是名门正派的眼中钉,不过对于我这种不想交钱看小说的人,没资格评论它,这个网站连载的小说更新的还是比较快的,内容都是和正版的内容一模一样.好了,废话不多说了,下面开始放代码: 我在抓取小说内容时先用了 requests 库来抓取,结果就抓到了一章小说的开头

用爬虫爬取笔趣阁小说

#时间 2019年3月4日19:16:06 #功能:爬取笔趣阁任何小说. from urllib import request from bs4 import BeautifulSoup #此函数用来获取每章对应的小说,并保存小说 def secondOpenURL(url,ch_name): # 请求每章详细内容 date = request.urlopen(url).read().decode('gbk') soup = BeautifulSoup(date, 'html.parser').

scrapycrawl 爬取笔趣阁小说

前言 第一次发到博客上..不太会排版见谅 最近在看一些爬虫教学的视频,有感而发,大学的时候看盗版小说网站觉得很能赚钱,心想自己也要搞个,正好想爬点小说能不能试试做个网站(网站搭建啥的都不会...) 站点拥有的全部小说不全,只能使用crawl爬全站 不过写完之后发现用scrapy爬的也没requests多线程爬的快多少,保存也不好一本保存,由于scrapy是异步爬取,不好保存本地为txt文件,只好存mongodb            捂脸 下面是主代码 # -*- coding: utf-8 -

笔趣阁小说-雪中悍刀行-爬虫源代码

1 import re 2 import requests 3 from bs4 import BeautifulSoup 4 5 url = 'http://www.biquge6.com/11_11147/' 6 r = requests.get(url) 7 b = BeautifulSoup(r.content.decode('gbk')) 8 h = b.find_all(href = re.compile('/11_11147/')) #正则匹配属性值带有/104_104216/的h

爬取笔趣阁小说

<修罗武神>是在17K小说网上连载的网络小说,作者为善良的蜜蜂.小说讲述了一个少年从下界二等门派外门弟子成长为上界翘楚人物的故事.该书曾入选“第三届橙瓜网络文学奖”百强作品. 编程只是实现目的的工具. 所以重点是分析我们的需求. 获取小说目录页面是基本.这里有各个章节的链接,标题等等内容.这是我们需要的. 有了各个章节的链接,就需要进入其中获得各个章节的内容. 1.首先是爬取网站的内容 1 def get_content(url): 2 3 try: 4 headers = { 5 'User

python应用:爬虫框架Scrapy系统学习第四篇——scrapy爬取笔趣阁小说

使用cmd创建一个scrapy项目: scrapy startproject project_name (project_name 必须以字母开头,只能包含字母.数字以及下划线<underscorce>) 项目目录层级如下: 声明Item 声明我们可能用到的所有字段,包括管理字段等.管理字段可以让我们清楚何时(date).何地(url server)及如何(spider)执行爬去,此外,还可以自动完成诸如使item失效.规划新的抓取迭代或是删除来自有问题的爬虫的item. 管理字段 Pytho

多线程爬取笔趣阁免费小说全站爬取

import threading,os,time,requests,pymongo,refrom queue import Queuefrom lxml import etreefrom bs4 import BeautifulSoup as BPclient = pymongo.MongoClient(host='localhost',port=27017)mg = client['biquge']def get_fenlei(): """ 爬取图书全部分类 :return

Python 爬取笔趣看小说

# -*- coding:utf-8 -*- from bs4 import BeautifulSoup import requests import sys class DownLoader(object): def __init__(self): self.server = 'http://www.biqukan.com/' self.target = 'http://www.biqukan.com/0_790/' self.header = {'User-Agent': 'Mozilla/