python爬取小说
运行结果:

代码:
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
import os class NovelSpider:
def __init__(self):
self.start_url = 'https://www.biqukan.com/1_1680/' def get_novel(self):
response = requests.get(self.start_url)
soup = BeautifulSoup(response.text, 'html.parser')
div_chapter = soup.find(class_="listmain")
chapter_list = div_chapter.find_all('a')
chapter_list = chapter_list[12:]
chapter = []
chapter_num = len(chapter_list)
count = 0
print('《凡人修仙传仙界篇》开始下载:')
for cl in chapter_list:
chapter_dict = {}
chapter_name = cl.get_text()
chapter_dict['name'] = chapter_name
chapter_url = cl.get('href')
chapter_dict['value'] = 'https://www.biqukan.com' + chapter_url
if chapter_dict not in chapter:
chapter.append(chapter_dict)
print(f"已下载:{count}/{chapter_num}")
self.download_novel(chapter_dict)
count += 1 def parse_novel(self, url):
browser = webdriver.PhantomJS(executable_path=r'F:\Spider\novelSpider\phantomjs.exe')
browser.get(url)
soup = BeautifulSoup(browser.page_source, 'html.parser')
find_txt = soup.find(class_='showtxt')
# print(type(find_txt.get_text()))
return find_txt.get_text() def download_novel(self, data):
filename = data['name']
url = data['value']
txt = self.parse_novel(url) path = r"F:\Spider\novelSpider"
isExists = os.path.exists(path)
if not isExists:
os.mkdir(path)
else:
pass with open(path + f'\凡人修仙传仙界篇.txt', 'a', encoding='utf-8') as f:
f.write(f'{filename}\n\n')
f.write(txt)
f.write('\n======\n\n')
f.close() if __name__ == '__main__':
ns = NovelSpider()
ns.get_novel()
python爬取小说的更多相关文章
- 用Python爬取小说《一念永恒》
我们首先选定从笔趣看网站爬取这本小说. 然后开始分析网页构造,这些与以前的分析过程大同小异,就不再多叙述了,只需要找到几个关键的标签和user-agent基本上就可以了. 那么下面,我们直接来看代码. ...
- python爬取小说详解(一)
整理思路: 首先观察我们要爬取的页面信息.如下: 自此我们获得信息有如下: ♦1.小说名称链接小说内容的一个url,url的形式是:http://www.365haoshu.com/Book/Cha ...
- 详细记录了python爬取小说《元尊》的整个过程,看了你必会~
学了好几天的渗透测试基础理论,周末了让自己放松一下,最近听说天蚕土豆有一本新小说,叫做<元尊>,学生时代的我可是十分喜欢读天蚕土豆的小说,<斗破苍穹>相信很多小伙伴都看过吧.今 ...
- 用python爬取小说章节内容
在学爬虫之前, 最好有一些html基础, 才能更好的分析网页. 主要是五步: 1. 获取链接 2. 正则匹配 3. 获取内容 4. 处理内容 5. 写入文件 代码如下: #导入相关model fro ...
- python入门学习之Python爬取最新笔趣阁小说
Python爬取新笔趣阁小说,并保存到TXT文件中 我写的这篇文章,是利用Python爬取小说编写的程序,这是我学习Python爬虫当中自己独立写的第一个程序,中途也遇到了一些困难,但是最后 ...
- Python3爬取小说并保存到文件
问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...
- Python实战项目网络爬虫 之 爬取小说吧小说正文
本次实战项目适合,有一定Python语法知识的小白学员.本人也是根据一些网上的资料,自己摸索编写的内容.有不明白的童鞋,欢迎提问. 目的:爬取百度小说吧中的原创小说<猎奇师>部分小说内容 ...
- python爬虫——爬取小说 | 探索白子画和花千骨的爱恨情仇(转载)
转载出处:药少敏 ,感谢原作者清晰的讲解思路! 下述代码是我通过自己互联网搜索和拜读完此篇文章之后写出的具有同样效果的爬虫代码: from bs4 import BeautifulSoup imp ...
- python之爬取小说
继上一篇爬取小说一念之间的第一章,这里将进一步展示如何爬取整篇小说 # -*- coding: utf- -*- import urllib.request import bs4 import re ...
随机推荐
- LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees
LeetCode 652: 寻找重复的子树 Find Duplicate Subtrees 题目: 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两 ...
- Concepts & Implementation of PLs
http://perugini.cps.udayton.edu/teaching/courses/Spring2015/cps352/index.html#lecturenotes Programmi ...
- 深入selenium模块基础操作
selenium模块的基本操作 一.模拟浏览器 谷歌.Firefox.Safari等浏览器 browser=webdriver.Chrome() browser=webdriver.Firefox ...
- ES6-Symbol的用法 ,symbol在对象中的应用,改变值
ES6-Symbol的用法,,symbol在对象中的应用,改变值 let a = new String; let b = new Number; let c = new Boolean; let d ...
- 如何在在手机上安装linux(ubuntu )关键词:Termux
目录 Termux软件 @(如何在在手机上安装ubuntu 关键词:Termux) Termux软件 Termux是一款开源且不需要root,运行在Android终端上极其强大的linux模拟器. 首 ...
- 测试IP的一些网址
http://httpbin.org/ip http://ip111.cn http://test.abuyun.com https://www.whatismybrowser.com
- Python踩坑系列之使用redis报错:module 'redis' has no attribute 'Redis'问题
初次使用redis时,在链接Redis后,运行报错“module 'redis' has no attribute 'Redis' ”. 具体代码如下: import redis r = redis. ...
- Codeforces Round #593 (Div. 2)
传送门 A. Stones 签到. B. Alice and the List of Presents 单独考虑每个数的贡献即可. 答案为\((2^{m}-1)^n\). C. Labs 构造就类似于 ...
- 记一次job不跑的问题
就在昨晚app后台升级后,发现有个重要的job不跑了,导致业务无法正常的走下去.这时候心里慌得一批,但还好我们有多台服务器影响不大,屁话少说进入主题. 看了下job的实现,发现其使用了java.uti ...
- 201871010115-马北《面向对象程序设计(java)》第一周学习总结
博文正文开头格式:(3分) 项目 内容 这个作业属于哪个课程 <https://www.cnblogs.com/nwnu-daizh/> 这个作业的要求在哪里 <https://ww ...