爬虫练习-爬取起点中文网小说信息
前言:
爬取起点中文网全部小说基本信息,小说名、作者、类别、连载\完结情况、简介,并将爬取的数据存储与EXCEL表中
本文为整理代码,梳理思路,验证代码有效性——2019.12.15
环境:
Python3(Anaconda3)
PyCharm
Chrome浏览器
主要模块:
xlwt
lxml
requests
time
1.
爬取起点中文网全部小说首页及所需信息如下
2.
分析请求的网页
http://a.qidian.com/? page=1 # 第一页
http://a.qidian.com/? page=2 # 第二页
http://a.qidian.com/? page=3 # 第三页
...
通过观察发现,全部小说总共有五页,后面的无法正常访问到,那么我们构造列表解析式
PS:很奇怪的是,有近一百万本小说,最后仅只能爬取五页共计八十多本,当然这是后话了
urls = ['http://a.qidian.com/? page={}'.format(str(i)) for i in range(1, 5)]
3.
解析并获取数据,打开开发者工具查看可知每本小说的数据均在标签class为“all-img-list cf”的ul下的 li 中,我们可以先行将其提取出来方便后续的数据解析
# 定位大标签,以此循环
infos = selector.xpath('//ul[@class="all-img-list cf"]/li')for info in infos:title = info.xpath('div[2]/h4/a/text()')[0]author = info.xpath('div[2]/p[1]/a[1]/text()')[0]style_1 = info.xpath('div[2]/p[1]/a[2]/text()')[0]style_2 = info.xpath('div[2]/p[1]/a[3]/text()')[0]style = style_1+'·'+style_2complete = info.xpath('div[2]/p[1]/span/text()')[0]introduce = info.xpath('div[2]/p[2]/text()')[0].strip()word = info.xpath('div[2]/p[3]/span/text()')[0].strip('万字')info_list = [title, author, style, complete, introduce, word]
我们将解析出来的数据通通放入一个静态公有的列表中
# 把数据存入列表
all_info_list.append(info_list)
4.
将列表中的数据转储与Excel表中
与text或word文本格式不同,这里我们定义一个表头并写入excel表
在写入之前要先后分别创建工作簿(即Excel表)、工作表(Sheet表)
# 定义表头
header = ['title', 'author', 'style', 'complete', 'introduce', 'word']
# 创建工作簿
book = xlwt.Workbook(encoding='utf-8')
# 创建工作表
sheet = book.add_sheet('Sheet1')
for h in range(len(header)):# 写入表头sheet.write(0, h, header[h])
将文件按行列方式写入Excel表
i = 1 # 行数
for list in all_info_list:j = 0 # 列数# 写入爬虫数据for data in list:sheet.write(i, j, data)j += 1 # 列数加一,和写字一样,从左往右写入数据i += 1 # 这里就是换行的意思
最后保存excel文件
# 保存文件
book.save('xiaoshuo.xls')
至此爬取起点中文网小说信息结束
A.完整代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-# 导入相应的库文件
import xlwt
import requests
from lxml import etree
import time# 初始化列表,存入爬虫数据
all_info_list = []# 定义获取爬虫信息的函数
def get_info(url):html = requests.get(url)selector = etree.HTML(html.text)# 定位大标签,以此循环infos = selector.xpath('//ul[@class="all-img-list cf"]/li')for info in infos:title = info.xpath('div[2]/h4/a/text()')[0]author = info.xpath('div[2]/p[1]/a[1]/text()')[0]style_1 = info.xpath('div[2]/p[1]/a[2]/text()')[0]style_2 = info.xpath('div[2]/p[1]/a[3]/text()')[0]style = style_1+'·'+style_2complete = info.xpath('div[2]/p[1]/span/text()')[0]introduce = info.xpath('div[2]/p[2]/text()')[0].strip()word = info.xpath('div[2]/p[3]/span/text()')[0].strip('万字')info_list = [title, author, style, complete, introduce, word]# 把数据存入列表all_info_list.append(info_list)# 睡眠1秒time.sleep(1)# 程序主入口
if __name__ == '__main__':urls = ['http://a.qidian.com/? page={}'.format(str(i)) for i in range(1, 5)]# 获取所有数据for url in urls:get_info(url)# 定义表头header = ['title', 'author', 'style', 'complete', 'introduce', 'word']# 创建工作簿book = xlwt.Workbook(encoding='utf-8')# 创建工作表sheet = book.add_sheet('Sheet1')for h in range(len(header)):# 写入表头sheet.write(0, h, header[h])i = 1 # 行数for list in all_info_list:j = 0 # 列数# 写入爬虫数据for data in list:sheet.write(i, j, data)j += 1i += 1# 保存文件book.save('xiaoshuo.xls')
B.所得数据
爬虫练习-爬取起点中文网小说信息相关推荐
- java爬虫抓取起点小说_爬虫实践-爬取起点中文网小说信息
qidian.py: import xlwt import requests from lxml import etree import time all_info_list = [] def get ...
- python 爬虫抓取网页数据导出excel_Python爬虫|爬取起点中文网小说信息保存到Excel...
前言: 爬取起点中文网全部小说基本信息,小说名.作者.类别.连载\完结情况.简介,并将爬取的数据存储与EXCEL表中 环境:Python3.7 PyCharm Chrome浏览器 主要模块:xlwt ...
- python爬虫之爬取起点中文网小说
python爬虫之爬取起点中文网小说 hello大家好,这篇文章带大家来制作一个python爬虫爬取阅文集团旗下产品起点中文网的程序,这篇文章的灵感来源于本人制作的一个项目:电脑助手 启帆助手 ⬆是项 ...
- Python爬虫之爬取起点中文网
python之爬取起点中文网 最近学了爬虫,想实战一下就选取了最近经常看小说的起点中文网来进行爬取 过程如下: 分析爬取信息: 爬取网址:https://www.qidian.com/rank?chn ...
- python爬虫——爬取起点中文网作品信息
首先打开起点中文网 点开红圈内的全部作品选项,本博客爬取这里面的作品信息. 接下来爬取所有作品信息,注意,不仅仅只是该面的所有作品信息,而是全部作品信息. 网页下面有跳转其他页的选项. 我们需要找到网 ...
- python request 爬虫爬取起点中文网小说
1.网页分析.进入https://www.qidian.com/,点击全部,进行翻页,你就会发现一个规律, url=https://www.qidian.com/all?orderId=&st ...
- 爬虫实战01--爬取起点中文网小说
废话不多说,直接上代码??? """ 实战:起点中文网--万古最强宗师(限免)1802章爬取 url: https://book.qidian.com/info/1012 ...
- python爬虫,爬取起点网站小说
使用python再来做一次爬虫:主要抓取玄幻类型的小说 目标网址:起点 使用模块:bs4,os模块 基本思路: 获取需求页面的元素代码,装到bs4容器里面,然后进行操作 首先获取接口:https:// ...
- Python简单爬取起点中文网小说(仅学习)
目录 前言 一.爬虫思路 二.使用步骤 1.引入库 2.读取页面 3.分析HTML 3.从标签中取出信息 4.爬取正文 总结 前言 实习期间自学了vba,现在开始捡回以前上课学过的python,在此记 ...
- 爬取起点中文网小说介绍信息
字数的信息(word)没有得到缺失 import xlwt import requests from lxml import etree import timeall_info_list=[] hea ...
最新文章
- 国外发明的10大仿生机械,这才是真正的黑科技!
- java2d简介_AWT_Java2D简介
- java.io.IOException: Unable to open sync connection!的解决方案
- 5-11attention网络结构
- 数据库入门-pymysql模块的使用
- 医学遗传学词汇英语术语英文(Glossary) 5
- Opengl ES Glew库 ----- By YDD的铁皮锅
- 深度学习-43:长短时记忆神经网络(LSTM)
- 一个初一蒟蒻的NOIP2020游记
- DirectX的发展历程!
- 设计模式之工厂方法模式(附源码)
- SOA对话:金融风暴不会阻碍SOA市场发展
- Android 仿朋友圈单张图片限定宽高超出时按比例缩放效果实现
- 如何批量打印流水且重复的条码数据
- 企业应要求SaaS云服务商提供的三类安全措施
- BZOJ 4430 Guessing Camels赌骆驼
- lsof Linux
- 解决Sklearn ValueError: empty vocabulary; perhaps the documents only contain stop words
- 基于Spyder(Python 3.8)网络爬虫东方财经股票数据
- 记录从站测试的一件糗事