还不知道要看什么小说嘛?爬取小说网站前10页的小说数据分析一波

效果

在这里插入图片描述

网页分析

网页网址分析

在这里插入图片描述
在这里插入图片描述
对比我们可以发现,不同的网页只有后边的数字不一样。
得到前10页的网址:

urls = ['https://www.qidian.com/all/page{}/'.format(str (i)) for i in range(1,11)]

书内容位置分析

在这里插入图片描述
在这里插入图片描述
对比我们可以得到页面上不同的小说,都是在同一个<ul><li>里边。
得ul到的XPath后//*[@id="book-img-text"]/ul 在后边选择li 即可

#选择 <ul>节点中的所有《li>节点
    infos = selector.xpath('//*[@id="book-img-text"]/ul/li')

不同书内容位置分析

第一本书的标题的Xpath://*[@id="book-img-text"]/ul/li[1]/div[2]/h4/a

第二本书的标题的Xpath://*[@id="book-img-text"]/ul/li[2]/div[2]/h4/a

我们发现只有 ==li[ ]==中的小标不一样,于是有:

title = info.xpath('//*[@id="book-img-text"]/ul/li['+str(i)+']/div[2]/h4/a/text()')[0]

通过i的变化来达到切换的目的。

将内容存到Excel

需要使用第三方库:

pip install wlwt

使用步骤:

  1. 导入库:import xlwt
  2. 创建Worbook 对象,并指定编码:book = xlwt.Workbook(encoding='utf-8')
  3. 添加Sheet :sheet = book.add_sheet('novels')
  4. 向Sheet 的Cell(1,1)位置添加文本:sheet.write(1,1,'世界,你好')
  5. 保存文件:book.save('novels.xls')

完整代码

import requests
from lxml import etree
import  xlwt
import  time

headers = {
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
'Host' : 'www.qidian.com',
'Cookie':'_ga_PFYW0QLV3P=GS1.1.1629617197.2.1.1629617333.0'
}

#//*[@id="book-img-text"]/ul/li[2]/div[2]/h4/a
def getOnePage(url):
    html = requests.get(url, headers=headers, allow_redirects=False)
    selector = etree.HTML(html.text)
    #选择 <ul>节点中的所有《li>节点
    infos = selector.xpath('//*[@id="book-img-text"]/ul/li')
    print(infos)
    result = []
    i = 1
    pre = '//*[@id="book-img-text"]/ul/li['
    for info in infos:
        # 注意的地方一 后边加[0]才能的字符串
        style_1 = info.xpath('//*[@id="book-img-text"]/ul/li['+str(i)+']/div[2]/p[1]/a[2]/text()')[0]
        style_2 = info.xpath('//*[@id="book-img-text"]/ul/li['+str(i)+']/div[2]/p[1]/a[3]/text()')[0]
        # 提取标题
        title = info.xpath('//*[@id="book-img-text"]/ul/li['+str(i)+']/div[2]/h4/a/text()')[0]
        # 提取作者
        author = info.xpath('//*[@id="book-img-text"]/ul/li['+str(i)+']/div[2]/p[1]/a[1]/text()')[0]
        # 风格
        style = style_1 +'.'+style_2
        # 完成度
        complete = info.xpath('//*[@id="book-img-text"]/ul/li['+str(i)+']/div[2]/p[1]/span/text()')[0]
        #简介
        introduce = info.xpath('//*[@id="book-img-text"]/ul/li['+str(i)+']/div[2]/p[2]/text()')[0]
        # 创建一个字典对象存入
        data = { 'title':title,'author':author,'style':style,'complete':complete,'introduce':introduce}
        result.append(data)
        # 换到下一本书
        i+=1
    print(result)
    return result


#
header = ['标题','作者','类型','完成度','介绍']

book = xlwt.Workbook(encoding='utf-8')

sheet = book.add_sheet('novels')

for h in range(len(header)):
    sheet.write(0,h,header[h])

#getOnePage('https://www.qidian.com/all/')
# 注意的地方二  /不能少
urls = ['https://www.qidian.com/all/page{}/'.format(str (i)) for i in range(1,11)]
i=1
#urls = ['https://www.qidian.com/all/']
for url in urls:
    novels = getOnePage(url)
    print(novels)
    for novel in novels:
        print(novel)
        time.sleep(0.1)
        sheet.write(i,0,novel['title'])
        sheet.write(i, 1, novel['author'])
        sheet.write(i, 2, novel['style'])
        sheet.write(i, 3, novel['complete'])
        sheet.write(i, 4, novel['introduce'])
        i+=1
book.save('novels.xls')

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落春只在无意间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值