实验题目:网络内容的抓取

本文介绍了如何使用Python的requests和BeautifulSoup库从xkcd.com网站抓取所有漫画图片,分别通过迭代和递归方法实现,并对比了两者的效果。
摘要由CSDN通过智能技术生成

计算机类专业实验报告

姓名:    学号:     班级:

实验题目:网络内容的抓取

  • 实验目的及意义

从网站https://xkcd.com中下载所有的漫画,并保存在当地磁盘上。

  • 实验相关知识点及要求
    1. requests模块的使用
    2. beautifulsoup模块的使用
    3. 程序需要使用迭代方式和递归两种不同的方式进行书写。
  • 实验步骤
    1. 实验各步骤具体内容

实验代码:

import os

import requests

from bs4 import BeautifulSoup

# 保存目录

os.makedirs('wmy1', exist_ok=True)

# 迭代方法

for i in range(1, 2001):

    # 构建网页URL

    url = f'https://xkcd.com/{i}/'

    # 发送HTTP请求获取网页内容

    response = requests.get(url)

    response.raise_for_status()

    # 解析HTML页面

    soup = BeautifulSoup(response.text, 'html.parser')

    # 查找漫画图片标签并下载保存

    comic_element = soup.select('#comic img')

    if comic_element:

        comic_url = 'https:' + comic_element[0]['src']

        comic_response = requests.get(comic_url)

        comic_response.raise_for_status()

        # URL中提取图片文件名

        filename = os.path.join('wmy1', os.path.basename(comic_url))

        # 保存图片到本地磁盘

        with open(filename, 'wb') as file:

            file.write(comic_response.content)

            print(f'Saved comic: {filename}')

    else:

        print(f'在该地址查找漫画图片时发生错误: {url}')

"""

# 递归方法

def download_comic(url):

    response = requests.get(url)

    response.raise_for_status()

    # 解析HTML页面

    soup = BeautifulSoup(response.text, 'html.parser')

    # 查找漫画图片标签并下载保存

    comic_element = soup.select('#comic img')

    if comic_element:

        comic_url = 'https:' + comic_element[0]['src']

        comic_response = requests.get(comic_url)

        comic_response.raise_for_status()

        # URL中提取图片文件名

        filename = os.path.join('wmy', os.path.basename(comic_url))

        # 保存图片到本地磁盘

        with open(filename, 'wb') as file:

            file.write(comic_response.content)

            print(f'Saved comic: {filename}')

    else:

        print(f'Comic image not found on the page: {url}')

    # 递归调用自身,下载下一页的漫画

    next_link = soup.select('a[rel="next"]')

    if next_link:

        next_url = 'https://xkcd.com' + next_link[0]['href']

        download_comic(next_url)

"""

 # 调用递归函数开始下载漫画

 #download_comic('https://xkcd.com/1/')

  • 实验结论

递归方法:

迭代方法

结果一致,学到了两种方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

光而不耀-2001

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

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

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

打赏作者

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

抵扣说明:

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

余额充值