Python爬取起点中文网小说信息及封面图片

网站网址

https://www.qidian.com/all

在这里插入图片描述
共有5个页面
在这里插入图片描述

分析

在这里插入图片描述

在这里插入图片描述
但是发现这个图片很小,那怎么办呢?
经过我的研究后发现,去掉地址后面的"/150",就好了
在这里插入图片描述
完美!!!

代码

我是在桌面创建了一个名为“爬取起点中文网”,的文件夹,然后把py文件放在里面运行。所以在你运行我的代码的时候,需要更改以下代码,更改为你自己的文件路径

os.chdir(r"C:\Users\dell\Desktop\爬取起点中文网")

同样你也可以修改存储图片的路径。

当然py文件的名称可以随意定义,不会影响程序的运行

完整代码如下:

# 导入相应的库文件
import xlwt
import requests
from lxml import etree
import os


# 初始化列表,存入爬虫数据
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_2
        complete = info.xpath('div[2]/p[1]/span/text()')[0]
        introduce = info.xpath('div[2]/p[2]/text()')[0].strip()
        
        info_list = [title, author, style, complete, introduce]
        # 把数据存入列表
        all_info_list.append(info_list)

        # 爬取小说封面图片
        
        if not os.path.exists('./picture'):
            os.mkdir('./picture')
        img_src='https:'+info.xpath('div[1]/a/img/@src')[0]
        img_src=img_src[0:-4]
        img_name=title+'.jpg'
        img_data=requests.get(img_src).content
        img_path='picture/'+img_name

        with open(img_path,'wb') as fp:
            fp.write(img_data)
            print(img_name,"下载成功")
    os.getcwd()
    os.chdir(r"C:\Users\dell\Desktop\爬取起点中文网")
    
# 程序主入口
if __name__ == '__main__':
    
    urls = ['http://a.qidian.com/? page={}'.format(str(i)) for i in range(1, 6)]
    # 获取所有数据
    print("开始爬取起点中文网小说封面图片......")
    for url in urls:
        get_info(url)
    print("起点中文网小说封面图片爬取完毕!")
    print("开始爬取起点中文网小说信息......")
    # 定义表头
    header = ['title', 'author', 'style', 'complete', 'introduce']
    # 创建工作簿
    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 += 1
        i += 1
    # 保存文件
    book.save('xiaoshuo.xls')
    print("起点中文网小说信息爬取完毕!")

爬取结果

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

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
好的,这里提供一个简单的Python爬取起点中文网月票榜数据并生成可视化图的示例代码,需要使用到requests、beautifulsoup4和matplotlib库。 首先,我们需要发送HTTP请求获取月票榜页面的HTML源码,然后使用beautifulsoup4库解析HTML源码,提取有用的数据。代码如下: ```python import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt # 发送HTTP请求,获取月票榜页面的HTML源码 url = 'https://www.qidian.com/rank/yuepiao' response = requests.get(url) html = response.text # 使用beautifulsoup4库解析HTML源码,提取有用的数据 soup = BeautifulSoup(html, 'html.parser') book_list = soup.select('.book-mid-info') titles = [] month_tickets = [] for book in book_list: title = book.select_one('.title').text.strip() titles.append(title) month_ticket = int(book.select_one('.month-ticket').text.strip()) month_tickets.append(month_ticket) ``` 接下来,我们可以使用matplotlib库将月票榜数据可视化,绘制条形图或饼图等不同类型的图表。代码如下: ```python # 使用matplotlib库将月票榜数据可视化,绘制条形图 plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置中文显示 plt.bar(range(len(titles)), month_tickets) plt.xticks(range(len(titles)), titles, rotation=90) plt.title('起点中文网月票榜') plt.xlabel('小说名称') plt.ylabel('月票数') plt.show() # 使用matplotlib库将月票榜数据可视化,绘制饼图 plt.pie(month_tickets, labels=titles, autopct='%1.1f%%') plt.title('起点中文网月票榜') plt.show() ``` 通过这段代码,我们可以爬取起点中文网月票榜数据,并将数据可视化为条形图或饼图,更加直观地了解小说的排名情况和月票数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

数据攻城小狮子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值