本章学习内容:将网站上的小说都爬下来,存储到本地。

目标网站:www.cuiweijuxs.com

分析页面,发现一共4步:从主页进入分版打开分页列表、打开分页下所有链接、打开作品页面、打开单章内容。

所以实现步骤如下:

1、进入分版页面,www.cuiweijuxs.com/jingpinxiaoshuo/

找到最大分页数

<a href="http://www.cuiweijuxs.com/jingpinxiaoshuo/5_122.html" class="last">122</a>

循环打开每个页面

href="http://www.cuiweijuxs.com/jingpinxiaoshuo/5_?.html" 

2、找到当页所有链接,循环打开单页链接,下为可定位元素

div id="newscontent"
div class="l"
  <span class="s2">
  <a href="http://www.cuiweijuxs.com/4_4521/" target="_blank">标题</a>

3、打开单页链接,找到章节列表,下为可定位元素

<div id="list">
<dd>
<a href="/4_4508/528170.html">第一章</a>
</dd>
</div>

4、打开单章链接,读取内容

<div id="content">

内容
<div>

 

setup1:创建class,初始化参数,抽象化获取beautifulsoup解析后到网页

# -*- coding: UTF-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import os '''
使用BeautifulSoup抓取网页
''' class Capture(): def __init__(self):
self.index_page_url = 'http://www.cuiweijuxs.com/'
self.one_page_url = 'http://www.cuiweijuxs.com/jingpinxiaoshuo/'
self.two_page_url = "http://www.cuiweijuxs.com/jingpinxiaoshuo/5_?.html"
self.folder_path = '小说/'
self.head = {}
# 写入User Agent信息
self.head[
'User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' # 获取BeautifulSoup
def getSoup(self, query_url):
req = request.Request(query_url, headers=self.head)
webpage = request.urlopen(req)
html = webpage.read()
#soup = BeautifulSoup(html, 'html.parser')
soup = BeautifulSoup(html, 'html5lib')
return soup
# end getSoup

  

setup2:创建进入分版页面,找到最大分页数,并循环打开每个页面

# 读取更新列表
def readPageOne(self):
soup = self.getSoup(self.one_page_url)
last = soup.find("a","last")
itemSize = int(last.string)
page_url = str(self.two_page_url) for item in range(itemSize):
print( item )
new_page_url = page_url.replace( "?",str(item+1) )
self.readPageTwo(new_page_url) # end readPageOne

  使用getSoup方法获取解析后到html网页,使用find方法找到class是“last”的a标签,获取最大分页数

  循环分页,从1开始

setup3:读取单页链接

#读取单页链接
def readPageTwo(self,page_url):
soup = self.getSoup(page_url)
con_div = soup.find('div',{'id':'newscontent'}).find('div',{'class':'l'})
a_list = con_div.find_all('span',{'class':'s2'})[0].find_all('a')
print(a_list)
for a_href in a_list:
#print(child)
href = a_href.get('href')
folder_name = a_href.get_text()
print('a_href',href,'---folder_name',folder_name)
path = self.folder_path + folder_name
self.createFolder(path)
self.readPageThree(href,path)
# end for # end readPageTwo

  找到div下id是newscontent的标签,再往下找到class是“l”的div,再找到所有class是“s2”的span,找到此span下的a标签,循环打开a标签

并找到标签名( a_href.get_text() )作为文件夹名称

setup4:打开作品页面,循环章节链接,拼接文件名称

   #打开作品页面
def readPageThree(self,page_url,path):
soup = self.getSoup(page_url)
print('readPageThree--',page_url)
a_list = soup.find('div', {'id': 'list'}).find_all('a')
idx = 0
for a_href in a_list:
idx = idx+1
href = self.index_page_url + a_href.get('href')
txt_name = path + '/' + str(idx) + '_'+ a_href.get_text() + '.txt'
print('a_href', href, '---path', txt_name)
isExists = os.path.exists(txt_name)
if isExists:
print(txt_name, '已存在')
else:
self.readPageFour(href,txt_name)

  

setup5:打开章节链接,读取id=content的div下所有内容,写入文件中

 #读取单章内容并写入
def readPageFour(self,page_url,path):
soup = self.getSoup(page_url)
con_div = soup.find('div', {'id': 'content'})
content = con_div.get_text().replace('<br/>', '\n').replace(' ', ' ')
self.writeTxt(path,content)

完整代码实现如下:

 # -*- coding: UTF-8 -*-
from urllib import request
from bs4 import BeautifulSoup
import os '''
使用BeautifulSoup抓取网页
''' class Capture(): def __init__(self):
self.index_page_url = 'http://www.cuiweijuxs.com/'
self.one_page_url = 'http://www.cuiweijuxs.com/jingpinxiaoshuo/'
self.two_page_url = "http://www.cuiweijuxs.com/jingpinxiaoshuo/5_?.html"
self.folder_path = '小说/'
self.head = {}
# 写入User Agent信息
self.head[
'User-Agent'] = 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19' # 获取BeautifulSoup
def getSoup(self, query_url):
req = request.Request(query_url, headers=self.head)
webpage = request.urlopen(req)
html = webpage.read()
#soup = BeautifulSoup(html, 'html.parser')
soup = BeautifulSoup(html, 'html5lib')
return soup
# end getSoup #读取更新列表
def readPageOne(self):
soup = self.getSoup(self.one_page_url)
last = soup.find("a","last")
itemSize = int(last.string)
page_url = str(self.two_page_url) for item in range(itemSize):
print( item )
new_page_url = page_url.replace( "?",str(item+1) )
self.readPageTwo(new_page_url) # end readPageOne #读取单页链接
def readPageTwo(self,page_url):
soup = self.getSoup(page_url)
con_div = soup.find('div',{'id':'newscontent'}).find('div',{'class':'l'})
a_list = con_div.find_all('span',{'class':'s2'})[0].find_all('a')
print(a_list)
for a_href in a_list:
#print(child)
href = a_href.get('href')
folder_name = a_href.get_text()
print('a_href',href,'---folder_name',folder_name)
path = self.folder_path + folder_name
self.createFolder(path)
self.readPageThree(href,path)
# end for # end readPage #打开单章链接
def readPageThree(self,page_url,path):
soup = self.getSoup(page_url)
print('readPageThree--',page_url)
a_list = soup.find('div', {'id': 'list'}).find_all('a')
idx = 0
for a_href in a_list:
idx = idx+1
href = self.index_page_url + a_href.get('href')
txt_name = path + '/' + str(idx) + '_'+ a_href.get_text() + '.txt'
print('a_href', href, '---path', txt_name)
isExists = os.path.exists(txt_name)
if isExists:
print(txt_name, '已存在')
else:
self.readPageFour(href,txt_name) #读取单章内容并写入
def readPageFour(self,page_url,path):
soup = self.getSoup(page_url)
con_div = soup.find('div', {'id': 'content'})
content = con_div.get_text().replace('<br/>', '\n').replace('&nbsp;', ' ')
self.writeTxt(path,content) def readPageHtml(self,page_url,path):
soup = self.getSoup(page_url)
con_div = soup.find('div', {'id': 'content'})
content = con_div.get_text().replace('<br/>', '\n').replace('&nbsp;', ' ') def createFolder(self,path):
path = path.strip()
# 去除尾部 \ 符号
path = path.rstrip("\\")
isExists = os.path.exists(path)
# 不存在则创建
if not isExists:
os.makedirs(path)
print(path + ' create')
else:
print( path + ' 目录已存在')
#end createFolder def writeTxt(self,file_name,content):
isExists = os.path.exists(file_name)
if isExists:
print(file_name,'已存在')
else:
file_object = open(file_name, 'w',encoding='utf-8')
file_object.write(content)
file_object.close() def run(self):
try:
self.readPageOne()
except BaseException as error:
print('error--',error) Capture().run()

python3+beautifulSoup4.6抓取某网站小说(三)网页分析,BeautifulSoup解析的更多相关文章

  1. python3+beautifulSoup4.6抓取某网站小说(一)爬虫初探

    本次学习重点: 1.使用urllib的request进行网页请求,获取当前url整版网页内容 2.对于多级抓取,先想好抓取思路,再动手 3.BeautifulSoup获取html网页中的指定内容 4. ...

  2. python3+beautifulSoup4.6抓取某网站小说(四)多线程抓取

    上一篇多文章,是二级目录,根目录"小说",二级目录"作品名称",之后就是小说文件. 本篇改造了部分代码,将目录设置为根目录->作者目录->作品目录- ...

  3. python3+beautifulSoup4.6抓取某网站小说(二)基础功能设计

    本章学习内容:1.网页编码还原读取2.功能设计 stuep1:网页编码还原读取 本次抓取对象: http://www.cuiweijuxs.com/jingpinxiaoshuo/ 按照第一篇的代码来 ...

  4. Python多进程方式抓取基金网站内容的方法分析

    因为进程也不是越多越好,我们计划分3个进程执行.意思就是 :把总共要抓取的28页分成三部分. 怎么分呢? # 初始range r = range(1,29) # 步长 step = 10 myList ...

  5. Python3利用BeautifulSoup4批量抓取站点图片的代码

    边学边写代码,记录下来.这段代码用于批量抓取主站下所有子网页中符合特定尺寸要求的的图片文件,支持中断. 原理很简单:使用BeautifulSoup4分析网页,获取网页<a/>和<im ...

  6. Python3.x+Fiddler抓取APP数据

    随着移动互联网的市场份额逐步扩大,手机APP已经占据我们的生活,以往的数据分析都借助于爬虫爬取网页数据进行分析,但是新兴的产品有的只有APP,并没有网页端这对于想要提取数据的我们就遇到了些问题,本章以 ...

  7. Python3.x:抓取百事糗科段子

    Python3.x:抓取百事糗科段子 实现代码: #Python3.6 获取糗事百科的段子 import urllib.request #导入各类要用到的包 import urllib import ...

  8. 使用BurpSuite抓取HTTPS网站的数据包

    昨天面试,技术官问到了我如何使用BurpSuite抓取https网站的数据包,一时间没能回答上来(尴尬!).因为以前https网站的数据包我都是用Fiddler抓取的,Fiddlert自动帮我们配置好 ...

  9. sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取

    原文:sqlserver 抓取所有执行语句 SQL语句分析 死锁 抓取 在多人开发中最头疼的是人少事多没有时间进行codereview,本来功能都没时间写,哪有时间来开会细细来分析代码.软件能跑就行, ...

随机推荐

  1. bzoj 1610: [Usaco2008 Feb]Line连线游戏【瞎搞】

    阴沟翻船.jpg 居然忘了除0的情况 枚举两两之间的线,把斜率装起来排个序去个重就好了 真是水的一晚上呢 #include<iostream> #include<cstdio> ...

  2. thinkphp数据库查重方法

    $test_data = M('hot'); //实例化数据表 $data = $test_data->Distinct(true)->field('descriprion')->o ...

  3. [SDOI2019] 移动金币

    分析 阶梯NIM模型:共有m+1堆石子,石子总数不超过n-m,求必胜的,即奇数堆石子数目异或和非零的局面数.补集转化,答案C(n,m)-奇数堆石子数目异或和位0的局面数. 可以想到按位dp,设f[i, ...

  4. Mac怎么刷新DNS缓存

    OS X Mavericks.Mountain Lion 和 Lion 请使用以下“终端”命令来还原 DNS 缓存设置: sudo killall -HUP mDNSResponder

  5. [ZPG TEST 114] 阿狸的英文名【水题】

    1.      阿狸的英文名 阿狸最近想起一个英文名,于是他在网上查了很多个名字.他发现一些名字可以由两个不同的名字各取一部分得来,例如John(约翰)的前缀 “John”和Robinson(鲁滨逊) ...

  6. assets与res/raw资源目录的区别

    1.简介 assets和res/raw工程目录下都可以放一些小于1M的文件(2.3版本以前要求,否则将不能读出数据.),这些文件将被原样打包到APK中应用使用. 2.不同 assets目录下的文件将原 ...

  7. Object C学习笔记18-SEL,@ selector,Class,@class--转

    一. SEL 类型 在上一篇介绍了几个方法,都只是介绍了其使用方式但是没有具体介绍参数: - (id)performSelector:(SEL)aSelector; - (id)performSele ...

  8. ambari集群里如何正确删除历史修改记录(图文详解)

    不多说,直接上干货! 答:这些你想删除的话得得去数据库里删除,最好别删除 .  现在默认就是使用好的配置               欢迎大家,加入我的微信公众号:大数据躺过的坑        人工智 ...

  9. AJPFX关于throw、throws关键字的解析

    throw.throws关键字 throw关键字: 是用于方法体内部,用来抛出一个Throwable类型的异常.如果抛出了检查异常, 则还应该在方法头部声明方法可能抛出的异常类型.该方法的调用者也必须 ...

  10. 学习笔记 第十一章 CSS3布局基础

    第11章   CSS3布局基础 [学习重点] 了解CSS2盒模型. 设计边框样式. 设计边界样式. 设计补白样式. 了解CSS3盒模型. 11.1  CSS盒模型基础 页面中所有元素基本显示形态为方形 ...