python 爬虫抓取网页数据导出excel_Python爬虫|爬取起点中文网小说信息保存到Excel...

前言:

爬取起点中文网全部小说基本信息,小说名、作者、类别、连载\完结情况、简介,并将爬取的数据存储与EXCEL表中

环境:Python3.7

PyCharm

Chrome浏览器

主要模块:xlwt

lxml

requests

time

起点中文网首页及所需信息如下:

5df8e2bc834b6.png

分析请求的网页http://a.qidian.com/? page=1#第一页http://a.qidian.com/? page=2#第二页http://a.qidian.com/? page=3#第三页...

构造列表解析式urls = ['http://a.qidian.com/? page={}'.format(str(i)) fori inrange(1, 5)]

5df8e39d08505.png

解析并获取数据,打开开发者工具查看可知每本小说的数据均在标签class为“all-img-list cf”的ul下的 li 中,我们可以先行将其提取出来方便后续的数据解析#定位大标签,以此循环infos = selector.xpath('//ul[@class="all-img-list cf"]/li')

forinfo ininfos:

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_2

complete = 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)

将列表中的数据转储与Excel表中

与text或word文本格式不同,这里我们定义一个表头并写入excel表

在写入之前要先后分别创建工作簿(即Excel表)、工作表(Sheet表)#定义表头header = ['title', 'author', 'style', 'complete', 'introduce', 'word']

#创建工作簿book = xlwt.Workbook(encoding='utf-8')

#创建工作表sheet = book.add_sheet('Sheet1')

forh inrange(len(header)):

#写入表头sheet.write(0, h, header[h])

将文件按行列方式写入Excel表i = 1#行数forlist inall_info_list:

j = 0#列数#写入爬虫数据fordata inlist:

sheet.write(i, j, data)

j += 1#列数加一,和写字一样,从左往右写入数据i += 1#这里就是换行的意思

最后保存excel文件#保存文件book.save('xiaoshuo.xls')

至此爬取起点中文网小说信息结束

完整代码#!/usr/bin/env python# -*- coding: utf-8 -*-#导入相应的库文件importxlwt

importrequests

fromlxml importetree

importtime

#初始化列表,存入爬虫数据all_info_list = []

#定义获取爬虫信息的函数defget_info(url):

html = requests.get(url)

selector = etree.HTML(html.text)

#定位大标签,以此循环infos = selector.xpath('//ul[@class="all-img-list cf"]/li')

forinfo ininfos:

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_2

complete = 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)) fori inrange(1, 5)]

#获取所有数据forurl inurls:

get_info(url)

#定义表头header = ['title', 'author', 'style', 'complete', 'introduce', 'word']

#创建工作簿book = xlwt.Workbook(encoding='utf-8')

#创建工作表sheet = book.add_sheet('Sheet1')

forh inrange(len(header)):

#写入表头sheet.write(0, h, header[h])

i = 1#行数forlist inall_info_list:

j = 0#列数#写入爬虫数据fordata inlist:

sheet.write(i, j, data)

j += 1i += 1#保存文件book.save('xiaoshuo.xls')

获得的数据:

5df8e4856329a.png

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值