爬取笔趣阁小说

《修罗武神》是在17K小说网上连载的网络小说,作者为善良的蜜蜂。小说讲述了一个少年从下界二等门派外门弟子成长为上界翘楚人物的故事。该书曾入选“第三届橙瓜网络文学奖”百强作品。

编程只是实现目的的工具。

所以重点是分析我们的需求。

获取小说目录页面是基本。这里有各个章节的链接,标题等等内容。这是我们需要的。

有了各个章节的链接,就需要进入其中获得各个章节的内容。

1.首先是爬取网站的内容

 1 def get_content(url):
 2
 3     try:
 4         headers = {
 5             ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36‘,
 6         }
 7
 8         r = requests.get(url=url, headers=headers)
 9         r.encoding = ‘utf-8‘
10         content = r.text
11         return content
12     except:
13         s = sys.exc_info()
14         print("Error ‘%s‘ happened on line %d" % (s[1], s[2].tb_lineno))
15         return " ERROR "

2.解析内容

 1 def praseContent(content):
 2     soup = BeautifulSoup(content,‘html.parser‘)
 3     chapter = soup.find(name=‘div‘,class_="bookname").h1.text
 4     content = soup.find(name=‘div‘,id="content").text
 5     save(chapter, content)
 6     next1 = soup.find(name=‘div‘,class_="bottem1").find_all(‘a‘)[2].get(‘href‘)
 7     # 如果存在下一个章节的链接,则将链接加入队列
 8     if next1 != ‘/0_638/‘:
 9         q.put(base_url+next1)
10     print(next1)

接下来就是完整代码

 1 import requests
 2 import time
 3 import sys
 4 import os
 5 import queue
 6 from bs4 import BeautifulSoup
 7 # 用一个队列保存url
 8 q = queue.Queue()
 9 # 首先我们写好抓取网页的函数
10 def get_content(url):
11
12     try:
13         headers = {
14             ‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36‘,
15         }
16
17         r = requests.get(url=url, headers=headers)
18         r.encoding = ‘utf-8‘
19         content = r.text
20         return content
21     except:
22         s = sys.exc_info()
23         print("Error ‘%s‘ happened on line %d" % (s[1], s[2].tb_lineno))
24         return " ERROR "
25
26 # 解析内容
27 def praseContent(content):
28     soup = BeautifulSoup(content,‘html.parser‘)
29     chapter = soup.find(name=‘div‘,class_="bookname").h1.text
30     content = soup.find(name=‘div‘,id="content").text
31     save(chapter, content)
32     next1 = soup.find(name=‘div‘,class_="bottem1").find_all(‘a‘)[2].get(‘href‘)
33     # 如果存在下一个章节的链接,则将链接加入队列
34     if next1 != ‘/0_638/‘:
35         q.put(base_url+next1)
36     print(next1)
37 # 保存数据到txt
38 def save(chapter, content):
39     filename = "修罗武神.txt"
40     f =open(filename, "a+",encoding=‘utf-8‘)
41     f.write("".join(chapter)+‘\n‘)
42     f.write("".join(content.split())+‘\n‘)
43     f.close
44
45 # 主程序
46 def main():
47     start_time = time.time()
48     q.put(first_url)
49     # 如果队列为空,则继续
50     while not q.empty():
51         content = get_content(q.get())
52         praseContent(content)
53     end_time = time.time()
54     project_time = end_time - start_time
55     print(‘程序用时‘, project_time)
56
57 # 接口地址
58 base_url = ‘https://www.xbiquge6.com‘
59 first_url = ‘https://www.xbiquge6.com/0_638/1124120.html‘
60 if __name__ == ‘__main__‘:
61     main()

学习爬取小说的过程还是很困难的,但成功的收获也很值得。

伴随着一些问题的解决,对于一些基本的操作也弄清楚了。对于这些东西的最好的学习方式,就是在使用中学习,通过解决问题的方式来搞定这些知识。按需索取,才能更有针对性。

原文地址:https://www.cnblogs.com/wt714/p/11963497.html

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

爬取笔趣阁小说的相关文章

用爬虫爬取笔趣阁小说

#时间 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').

Python 爬取笔趣阁小说

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

scrapycrawl 爬取笔趣阁小说

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

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/

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

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

免app下载笔趣阁小说

这个是对最近学习的一次总结吧.前两天写的,今天才有时间写博客. 偶然点开笔趣阁的网址(https://www.biquge.cc/),突然觉得我应该可以用爬虫实现小说下载.有这个想法我就开始尝试了. 爬虫呀,说白了就是程序自动模拟浏览器操作来获取网页的内容. 先用F12查看元素,查看章节网址链接,和章节正文内容. 结构很简单. 想法很快就有了,通过网站的搜索打开小说详情页,然后获取每一章的网址url,依次访问每一章网址,再通过正则表达式匹配章节内容, 最后将匹配的内容保存到本地. 中间忘了一个小

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

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