1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python 爬虫抓取网页数据导出excel_Python爬虫|爬取起点中文网小说信息保存到Excel...

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

时间:2021-06-12 14:22:13

相关推荐

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

前言:

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

环境:Python3.7

PyCharm

Chrome浏览器

主要模块:xlwt

lxml

requests

time

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

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

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

解析并获取数据,打开开发者工具查看可知每本小说的数据均在标签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 = ['/? 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')

获得的数据:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。