代码
import os import requests from bs4 import BeautifulSoup # 指定要爬取的网址 url = 'https://www.baidu.com/' # 请求网页内容 response = requests.get(url) response.raise_for_status() # 如果请求失败,将抛出异常 # 使用BeautifulSoup解析网页内容 soup = BeautifulSoup(response.text, 'html.parser') # 创建存储图片的文件夹 folder_name = r'F:\jingguan\tu' if not os.path.exists(folder_name): os.makedirs(folder_name) # 找到网页中的所有<img>标签 img_tags = soup.find_all('img') # 遍历所有的<img>标签,下载图片 for img in img_tags: src = img.get('src') # 获取图片的src属性 if src: # 完整的图片URL img_url = src if src.startswith(('http:', 'https:')) else url + src try: # 发送请求获取图片内容 img_response = requests.get(img_url) img_response.raise_for_status() # 图片文件名 img_name = os.path.join(folder_name, img_url.split('/')[-1]) with open(img_name, 'wb') as f: f.write(img_response.content) print(f"图片已下载:{img_name}") except requests.exceptions.RequestException as e: print(f"下载图片时出错:{e}") print("图片下载完成。")
标签:src,name,img,python,爬取,网址,url,response,图片 From: https://www.cnblogs.com/jingzaixin/p/18163719