python爬虫,以某小说网站为例
python的简单爬虫,通过获取网页的源代码分理处有用的数据再进行分析整理保存的过程。
大体可以分为三步:
- 获取网页源代码
- 整理剥离有用代码
- 储存信息
1、通过requests库获取网站源码
'''
Description: html工具类
Version: 1.0
Autor: 李广凯
Date: 2022-02-24 20:23:23
LastEditors: 李广凯
LastEditTime: 2022-04-04 22:48:56
'''
import requests
# 取得网页源码
def getHTML(start_url):html = requests.get(start_url)html.raise_for_status# html.encoding = 'utf-8'html.encoding = html.apparent_encodingreturn html.text
2、整理网页源代码
#获取排行榜标签:日排行、周排行、月排行
def getListTag(html):# 标签列表list_tag = []soup = BeautifulSoup(html, 'html.parser')useful = soup.find(class_='tab')li_list = useful.find_all('li')for li in li_list:list_tag.append(li.string)return list_tag# 获取排行榜的url,书名,封面
def getBookTopList(html):global list_book_toplist_tag = getListTag(html)list_book_url = []list_book_name = []list_book_img = []list_book_top = []soup = BeautifulSoup(html, 'html.parser')useful = soup.find(class_='tabCon')ul_context = useful.find_all('ul')for ul in ul_context:list_book_img_cache = []list_book_name_cache = []list_book_url_cache = []for li in ul:li = str(li).replace('\n', '')book_img = re.findall('src="(.*?)"', li)if len(book_img):list_book_img_cache.append(book_img[0])if len(li):book_url = re.search('href="(.*?)"', li).group(1)list_book_url_cache.append(book_url)book_name = re.search('title="(.*?)"', li).group(1)list_book_name_cache.append(book_name)list_book_img.append(list_book_img_cache)list_book_name.append(list_book_name_cache)list_book_url.append(list_book_url_cache)print(list_book_name)for i in range(len(list_tag)):for k in range(len(list_book_name[i])):dic_book_top_cache = {'榜单名': str(list_tag[i]),'书名': str(list_book_name[i][k]),'封面': str(list_book_img[i][k]),'url': str(list_book_url[i][k])}list_book_top.append(dic_book_top_cache)print('获取排行榜列表成功!')return list_book_top# 补充排行榜列表(作者、书籍简介)
def insertBookListTop():url_list = []for book in list_book_top:url_list.append(book['url'])pool = Pool(5)result = pool.map(getBookSimpleInfo, url_list)for i in range(len(result)):list_book_top[i]['作者'] = str(result[i][0][3:])list_book_top[i]['简介'] = str(result[i][1])list_book_top[i]['下载地址'] = str(result[i][2])#获取下载小说文件的url
def downloadBookFile(download_before_url):html = html_tool.getHTML(download_before_url)soup = BeautifulSoup(html, 'html.parser')useful = soup.find(class_='down-btn-group')a_context = useful.find('a')a_context = str(a_context).replace('\n', '')download_book_url = re.search('href="(.*?)"', a_context).group(1)return download_book_url# 添加书籍信息
def getBookSimpleInfo(url):html = html_tool.getHTML(url)soup = BeautifulSoup(html, 'html.parser')useful = soup.find(class_='info2')h3_context = useful.find('h3')p_context = useful.find('p')author = h3_context.stringinfo = p_context.string# 下载页面urla_context = soup.find(class_='btn btn-danger')a_context = str(a_context).replace('\n', '')download_before_url = re.search('href="(.*?)"', a_context).group(1)download_before_url = start_url + download_before_urldownload = downloadBookFile(download_before_url)info = str(info).replace('\n', '')info = str(info).replace(' ', '')return author, info, download
3、储存文件或插入数据库
#储存文件
def saveFile(book_info):url = book_info['下载地址']path = './book_file/' + book_info['书名'] + '.txt'r = requests.get(url)with open(path, "wb") as code:code.write(r.content)# urlretrieve(url,path)print(book_info['书名'] + '下载完成!')# 插入数据库
def insertDB(book):file_path = './book_file/' + book['书名'] + '.txt'datetime = time.strftime("%Y-%m-%d", time.localtime())if book['榜单名'] == '日排行':sql = "INSERT INTO day_top(bname,bimg,bauthor,binfo,bfile,bdate) VALUES(" + '"' + book['书名'] + '","' + book['封面'] + '","' + book['作者'] + '","' + book['简介'] + '","' + file_path + '","' + datetime + '")'if book['榜单名'] == '周排行':sql = "INSERT INTO week_top(bname,bimg,bauthor,binfo,bfile,bdate) VALUES(" + '"' + book['书名'] + '","' + book['封面'] + '","' + book['作者'] + '","' + book['简介'] + '","' + file_path + '","' + datetime + '")'if book['榜单名'] == '月排行':sql = "INSERT INTO month_top(bname,bimg,bauthor,binfo,bfile,bdate) VALUES(" + '"' + book['书名'] + '","' + book['封面'] + '","' + book['作者'] + '","' + book['简介'] + '","' + file_path + '","' + datetime + '")'db.insertData(sql)
因为只是获取排行榜数据,所以就用一个全局列表list_book_top[] 来临时存储信息。代码整体还是比较简单,就是分析提取书籍信息部分比较麻烦。
python爬虫,以某小说网站为例相关推荐
- python爬虫爬取小说网站并转换为语音文件
前言 作为一个技术宅,本着为眼睛着想的角度考虑,能少看屏幕就尽量少看屏幕,可是又是一个小说迷,那就开动脑筋爬一下小说转换成语音来听书吧 第一章:爬取小说文件 把目标定在小说存储量比较大的网站:起点中文 ...
- 利用Python爬虫抓取小说网站全部文章
我们先来选定爬取目标,我爬取的网站是https://www.17k.com/ ,一些大型的网站(如起点.豆瓣等)做了反爬虫的部署,这会大大增加我们抓取的难度,所以尽量还是选一些不那么热门的网站. 爬虫 ...
- python 小说cms系统_「博文小说网」Python爬虫爬取小说网站 - seo实验室
博文小说网 #!/usr/bin/env Python # -*- coding: utf-8 -*- # @Author : Woolei # @File : book136_singleproce ...
- 记第一次Python爬虫-----五块五毛网站小说
事情要从那个吃了蝙蝠的人开始说起(2020.02.03记)...... 一.前因 因为哪些个吃蝙蝠的人,让我有足够的时间和充分的理由,可以待在家不出门,也不会被嫌弃,于是,待久了,人总要无聊,于是开始 ...
- Python爬虫实战——下载小说
Python爬虫实战--下载小说 前言 第三方库的安装 示例代码 效果演示 结尾 前言 使用requests库下载开源网站的小说 注意:本文仅用于学习交流,禁止用于盈利或侵权行为. 操作系统:wind ...
- python爬虫实践--晋江小说书籍分析
系列文章目录 python爬虫实践–晋江小说书籍分析 python爬虫实践-腾讯视频弹幕分析 python爬虫实践-B站弹幕分析 文章目录 系列文章目录 目录 爬取晋江排行榜 爬取文案 其它想法 结语 ...
- python爬虫 多线程 爬取网站排行榜上的所有小说
python爬虫,多线程批量爬取多部小说 欢迎阅读 环境 获取排行榜top上的所有小说地址 分析排行榜top100页面的html,获取所有小说的url 遍历小说列表url到生成txt 多线程启动爬取任 ...
- python爬虫怎么爬同一个网站的多页数据-如何用Python爬数据?(一)网页抓取
如何用Python爬数据?(一)网页抓取 你期待已久的Python网络数据爬虫教程来了.本文为你演示如何从网页里找到感兴趣的链接和说明文字,抓取并存储到Excel. 需求 我在公众号后台,经常可以收到 ...
- python爬虫怎么爬小说_python从爬虫基础到爬取网络小说实例
一.爬虫基础 1.1 requests类 1.1.1 request的7个方法 requests.request() 实例化一个对象,拥有以下方法 requests.get(url, *args) r ...
- Python爬虫实战—vmgrils图片网站
一.实战背景 唯美女生:https://www.vmgirls.com/ 少女情怀总是诗,一双发现美的眼睛! 工具:Python3.7,PyCharm 程序所需用到的模块:requests,fake_ ...
最新文章
- 文件名有规则情况读取
- 链表问题3——删除链表的a/b处的节点(进阶)
- Struts 验证码登陆
- matlab inpainting,MATLAB-Python-inpainting-codes-master
- .net core 24
- hive UDF函数
- C#中如何获取鼠标及控件的位置坐标(相对于屏幕)
- php v9 邮箱登陆,PHPCMS v9会员登录支持Email登录的实现方法
- MFC dialog 间 交互[2]
- axure 发布 主页_车队如何在黑鸟APP发布同城活动?教程来了!
- 银行考试计算机重点知识,银行计算机考试试题
- 黑进mysql_连接到MySQL数据库
- android 蓝牙 发送字符串,Android向TLSR8266蓝牙mesh发送指令
- 6678运行 linux,Ubuntu下使用Acer6678-0BM扫描仪
- 极品五笔输入法2009_考场指南!2020年注会机考计算器使用指南及输入法切换
- Configured
- Java 计算两个日期时间差(天数、时、分、秒、毫秒)
- Python自做的一个zabbix_sender程序
- AD16快速上手指南(AD16为例)
- 儿童的31个“敏感期”特点及建议(转载)