金狮镖局 Design By www.egabc.com

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

前文

01、python爬虫入门教程01:豆瓣Top电影爬取

基本开发环境

  • Python 3.6
  • Pycharm

相关模块的使用

  • request
  • sparsel

安装Python并添加到环境变量,pip安装需要的相关模块即可。

Python爬虫入门教程02之笔趣阁小说爬取

单章爬取

Python爬虫入门教程02之笔趣阁小说爬取

一、明确需求

爬取小说内容保存到本地

  • 小说名字
  • 小说章节名字
  • 小说内容
# 第一章小说url地址
url = 'http://www.biquges.com/52_52642/25585323.html'
url = 'http://www.biquges.com/52_52642/25585323.html'
headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
print(response.text)

Python爬虫入门教程02之笔趣阁小说爬取

请求网页返回的数据中出现了乱码,这就需要我们转码了。

加一行代码自动转码。

response.encoding = response.apparent_encoding

Python爬虫入门教程02之笔趣阁小说爬取

三、解析数据

Python爬虫入门教程02之笔趣阁小说爬取

根据css选择器可以直接提取小说标题以及小说内容。

def get_one_novel(html_url):
 # 调用请求网页数据函数
 response = get_response(html_url)
 # 转行成selector解析对象
 selector = parsel.Selector(response.text)
 # 获取小说标题
 title = selector.css('.bookname h1::text').get()
 # 获取小说内容 返回的是list
 content_list = selector.css('#content::text').getall()
 # ''.join(列表) 把列表转换成字符串
 content_str = ''.join(content_list)
 print(title, content_str)

if __name__ == '__main__':
 url = 'http://www.biquges.com/52_52642/25585323.html'
 get_one_novel(url)

Python爬虫入门教程02之笔趣阁小说爬取

四、保存数据(数据持久化)

使用常用的保存方式: with open

def save(title, content):
 """
 保存小说
 :param title: 小说章节标题
 :param content: 小说内容
 :return: 
 """
 # 路径
 filename = f'{title}\\'
 # os 内置模块,自动创建文件夹
 if os.makedirs(filename):
 os.mkdir()
 # 一定要记得加后缀 .txt mode 保存方式 a 是追加保存 encoding 保存编码
 with open(filename + title + '.txt', mode='a', encoding='utf-8') as f:
 # 写入标题
 f.write(title)
 # 换行
 f.write('\n')
 # 写入小说内容
 f.write(content)

Python爬虫入门教程02之笔趣阁小说爬取
Python爬虫入门教程02之笔趣阁小说爬取

保存一章小说,就这样写完了,如果想要保存整本小说呢?

整本小说爬虫

既然爬取单章小说知道怎么爬取了,那么只需要获取小说所有单章小说的url地址,就可以爬取全部小说内容了。

Python爬虫入门教程02之笔趣阁小说爬取

所有的单章的url地址都在 dd 标签当中,但是这个url地址是不完整的,所以爬取下来的时候,要拼接url地址。

def get_all_url(html_url):
 # 调用请求网页数据函数
 response = get_response(html_url)
 # 转行成selector解析对象
 selector = parsel.Selector(response.text)
 # 所有的url地址都在 a 标签里面的 href 属性中 
 dds = selector.css('#list dd a::attr(href)').getall()
 for dd in dds:
 novel_url = 'http://www.biquges.com' + dd
 print(novel_url)


if __name__ == '__main__':
 url = 'http://www.biquges.com/52_52642/index.html'
 get_all_url(url)

Python爬虫入门教程02之笔趣阁小说爬取

这样就获取了所有的小说章节url地址了。

爬取全本完整代码

import requests
import parsel
from tqdm import tqdm


def get_response(html_url):
 headers = {
 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
 }
 response = requests.get(url=html_url, headers=headers)
 response.encoding = response.apparent_encoding
 return response


def save(novel_name, title, content):
 """
 保存小说
 :param title: 小说章节标题
 :param content: 小说内容
 :return:
 """
 filename = f'{novel_name}' + '.txt'
 # 一定要记得加后缀 .txt mode 保存方式 a 是追加保存 encoding 保存编码
 with open(filename, mode='a', encoding='utf-8') as f:
 # 写入标题
 f.write(title)
 # 换行
 f.write('\n')
 # 写入小说内容
 f.write(content)


def get_one_novel(name, novel_url):
 # 调用请求网页数据函数
 response = get_response(novel_url)
 # 转行成selector解析对象
 selector = parsel.Selector(response.text)
 # 获取小说标题
 title = selector.css('.bookname h1::text').get()
 # 获取小说内容 返回的是list
 content_list = selector.css('#content::text').getall()
 # ''.join(列表) 把列表转换成字符串
 content_str = ''.join(content_list)
 save(name, title, content_str)


def get_all_url(html_url):
 # 调用请求网页数据函数
 response = get_response(html_url)
 # 转行成selector解析对象
 selector = parsel.Selector(response.text)
 # 所有的url地址都在 a 标签里面的 href 属性中
 dds = selector.css('#list dd a::attr(href)').getall()
 # 小说名字
 novel_name = selector.css('#info h1::text').get()
 for dd in tqdm(dds):
 novel_url = 'http://www.biquges.com' + dd
 get_one_novel(novel_name, novel_url)

if __name__ == '__main__':
 novel_id = input('输入书名ID:')
 url = f'http://www.biquges.com/{novel_id}/index.html'
 get_all_url(url)

Python爬虫入门教程02之笔趣阁小说爬取
Python爬虫入门教程02之笔趣阁小说爬取

标签:
Python爬虫笔趣阁小说爬取,Python笔趣阁小说爬取

金狮镖局 Design By www.egabc.com
金狮镖局 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com

评论“Python爬虫入门教程02之笔趣阁小说爬取”

暂无Python爬虫入门教程02之笔趣阁小说爬取的评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。