首页 > 编程语言 >python爬虫抓取小说

python爬虫抓取小说

时间:2023-07-15 19:47:13浏览次数:48  
标签:chapter python 爬虫 抓取 re fb HTML print findall

我这里是使用的requests模块和re(正则)模块
可以模仿浏览器正常访问网页返回网页源码的方式,通过正则获取到小说的名字,以及每个章节名称和对应的网页链接,并将小说正文截取出来,写入到文本中,具体代码实现如下:

# 导入requests模块
import requests
# 导入re(正则)模块
import re

# 下载一个网页
url = 'https://ww.sangwu8.com/book/13/13962/'
# 模拟浏览器发送http请求
response = requests.get(url)
response.encoding = "gbk"
# 目标小说主页的网页源码
HTML = response.text

#print(HTML)
#print(re.findall(r'<h2>(.*?)</h2>', HTML))
# # 获取小说的名字
title = re.findall(r'<h2>(.*?)</h2>', HTML)[0]
print(title)
# # 新建一个文件,保存小说内容
fb = open('%s.txt' % title, 'w', encoding='utf-8')

# # 获取每一章的信息(章节,url)
# ul = re.findall(r'<dl>.*?</dl>', HTML, re.S)[0]
dl = re.findall(r'<dl>.*?</dl>', HTML, re.S)[0]
# print(dl)
# # [0]指列表

# # 提取 章节url、章节信息
chapter_info_list = re.findall(r'<a href="(.*?)">(.*?)<', dl)
# print(chapter_info_list)

# # 循环每一个章节,分别去下载
for chapter_info in chapter_info_list:
    chapter_url, chapter_title = chapter_info
    chapter_url = "https://ww.sangwu8.com/book/13/13962/%s" %chapter_url
    print(chapter_url, chapter_title)

    # 下载章节内容
    chapter_response = requests.get(chapter_url)
    chapter_response.encoding = 'gbk'
    chapter_HTML = chapter_response.text.replace('\u3000','').replace('&nbsp;','')\
        .replace('&lt;','').replace('br/&gt;','').replace('&amp;','').replace('lt;','').replace('gt;','').replace('<br />','').replace("\r", "").replace("\n", "")


    # print(chapter_HTML)
    # text = re.findall(r'<div class="centent">(.*?)</div>', chapter_HTML, re.S)
    # print(text)
    # 提取章节内容
    chapter_content = re.compile('.*?<div class="centent">(.*?)</div>', re.S)
    print(chapter_content)
    test = re.findall(chapter_content, chapter_HTML)
    #print(test)
    #break 单章测试时退出使用
    # 数据持久化
    fb.write('\n')
    fb.write(chapter_title)
    fb.write('\n')
    fb.write(test[0])

执行截图如下:
可以按照章节写入文本中
image

最终输出文本结果:
image

image

标签:chapter,python,爬虫,抓取,re,fb,HTML,print,findall
From: https://www.cnblogs.com/kyween/p/17556764.html

相关文章

  • Python练手小项目——简易版基础SQL模板代码生成器
    1、效果图2、代码源码-ui.py:fromtkinterimport*fromtkinterimportscrolledtext,messageboxfromtkinter.ttkimportComboboximportpymysqldefinit():#创建窗口:实例化一个窗口对象window=Tk()#窗口大小window.geometry("900x550")......
  • Python 并发编程之IO模型(转载)
    Python并发编程之IO模型https://www.cnblogs.com/linhaifeng/articles/7454717.htmlIO模型介绍为了更好地了解IO模型,我们需要事先回顾下:同步、异步、阻塞、非阻塞同步(synchronous)IO和异步(asynchronous)IO,阻塞(blocking)IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别......
  • Python 潮流周刊第 11 期(2023-07-15)
    查看全文:Python潮流周刊#11:如何使用Golang运行Python代码?......
  • python打印各种文本颜色及加粗、背景色、斜体、下划线
    ----------字体颜色------------print("\033[1;30m字体颜色:白色\033[0m")print("\033[1;31m字体颜色:红色\033[0m")print("\033[1;32m字体颜色:深黄色\033[0m")print("\033[1;33m字体颜色:浅黄色\033[0m")print("\033[1;34m字体颜色:蓝色\033[0m&quo......
  • python魔术方法属性篇
    python魔术方法属性篇本篇章主要讲与对象的属性有关的魔术方法3,属性篇__getattr____getattribute____setattr____delattr____dir____get____set____delete____slots__ __getattr__方法每当我们写形如这种o.test的代码的时候,我们实际上都是在尝试access这个对象......
  • Python pygame实现中国象棋单机版源码
    今天给大家带来的是关于Python实战的相关知识,文章围绕着用Pythonpygame实现中国象棋单机游戏版展开,文中有非常详细的代码示例,需要的朋友可以参考下#-*-coding:utf-8-*-"""CreatedonSunJun1315:41:562021@author:Administrator"""importpygamefrompygame.local......
  • PHP调用Python无返回或提示No Module
    问题:自己通过命令行执行python正常,但通过php调用就没有反应。解决方法:1、首先检查一下php有没有执行权限,简单粗暴的:sudochmod777xxx.php2、Python如果有中文返回,似乎需要额外操作。可以先去掉中文排除掉其他原因,也可以尝试以下操作:在python文件头部加上importcodecssys.stdout......
  • 【性能测试】性能监控-python编写(CPU | 内存 | 磁盘io)占比监控脚本
    一、主要通过Python脚本实现对linux环境(CPU|内存|磁盘io)监控脚本示例:importtimeimportsubprocess#获取CPU使用率defget_cpu_usage():#系统CPU占比=系统态占比+空闲态占比=3.2%+36.5%=39.7%cpu_usage=subprocess.check_output("top-bn1......
  • Python 20个魔法函数
    本文将为您详细介绍Python中的20个魔法函数,这些函数能够在代码中释放神奇的力量。让我们一起来了解这些特殊的函数,并提供一些在实际接口自动化工作中的示例代码。魔法函数(MagicMethods),也被称为特殊方法或双下划线方法,是Python中一些特殊命名的函数,它们以双下划线开头和结尾......
  • Python学习——Day 6
    流程控制语句break·break语句   ·用于结束循环结构,通常与分支结构if一起使用#输入密码,最多录入3次,如果正确就结束循环foriteminrange(3):pwd=input('请输入密码:')ifpwd=='8888':print('密码正确')breakelse:print('密码......