Python 爬虫练习 批量爬取导师信息与照片
import requests
from bs4 import BeautifulSoup
import os
def download_image(url, filename):
response = requests.get(url)
with open(filename, 'wb') as file:
file.write(response.content)
def get_mentors_info_and_images(url, directory='mentors_images'):
if not os.path.exists(directory):
os.makedirs(directory)
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
mentors = soup.find_all('div', class_='mentor-container')
for mentor in mentors:
name = mentor.find('h3', class_='mentor-name').text.strip()
title = mentor.find('h4', class_='mentor-title').text.strip()
image_url = mentor.find('img', class_='mentor-profile-pic')['src']
print(f'Name: {name}, Title: {title}, Image URL: {image_url}')
filename = os.path.join(directory, f"{name.replace(' ', '_')}.jpg")
download_image(image_url, filename)
if __name__ == '__main__':
url = 'https://www.example.com/mentors'
get_mentors_info_and_images(url)
这个代码示例修复了原始代码中的问题,并添加了错误处理和对导师图片的下载功能。它首先定义了一个下载图片的函数,该函数接受图片的URL和文件名作为参数,然后使用requests库获取图片内容并将其写入到本地文件。接下来,定义了一个获取导师信息和下载图片的函数,该函数首先检查目标目录是否存在,不存在则创建。然后,它发送GET请求获取网页内容,并使用BeautifulSoup解析网页。最后,它遍历每个导师容器,提取导师的姓名、头衔和图片URL,并打印信息。然后调用之前定义的下载图片的函数来下载图片。
评论已关闭