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

目标网站: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. less 语法

    1 变量 less的变量使用@开头 1.1 demo @colorRed:red; @colorBlue:blue; .demo{ color:@colorRed; background-color: ...

  2. asp.net mvc5 使用百度ueditor 本编辑器完整示例(下)配置上传播放视频

    通过 asp.net mvc5 使用百度ueditor 本编辑器完整示例(上)介绍,可以上传图片到服务器了,也可以上传小的视频文件,并且由百度编辑器自动加入html5<video>标签播放 ...

  3. Java多线程系列七——ExecutorService

    java.util.concurrent.ExecutorService接口提供了许多线程管理的方法 Method 说明 shutdown 拒绝接收新的任务,待已提交的任务执行后关闭,且宿主线程不阻塞 ...

  4. idea 设置项目编码

    目前我了解的设置idea编码有两种形式(但深层次不太了解) 方式一: 这个方式需要你点击要设置编码单个文件,选择编码.一次只能修改一个文件编码 方式二: 一般工作时使用这个方式,文件编码,选择项目文件 ...

  5. 【Aizu - 0005 】GCD and LCM

    GCD and LCM Descriptions: Write a program which computes the greatest common divisor (GCD) and the l ...

  6. mysql case 列名 when 和 case when的区别

    最近写了一个sql,才发现有些情况不能用case 列名 when ( and then and and 7.9 then '中' else '差' END ) score_type, 我发现这样写查出 ...

  7. 史上最详细最全的Linux上安装Oracle的教程-centos7

    一.安装Oracle前准备 1.创建运行oracle数据库的系统用户和用户组 [humf@localhost ~]$ su root #切换到root Password: [root@localhos ...

  8. 2017 JUST Programming Contest 3.0 D. Dice Game

    D. Dice Game time limit per test 1.0 s memory limit per test 256 MB input standard input output stan ...

  9. Linux环境下使用yum安装zip和unzip

    Linux环境下使用yum安装zip和unzip. yum install zip yum install unzip

  10. js中判断数据类型的方法 typeof

    <input type="text" onblur="demo(this)"/><br/> <input type="n ...