需要对网页图片进行爬取编写代码,要求保留图片名称,用代码在D盘新建一个文件夹,名称为pic捕获错误返回通信代码还有异常
时间: 2023-04-10 22:03:31 浏览: 84
可以使用Python的requests和BeautifulSoup库来实现对网页图片的爬取和保存。以下是示例代码:
```python
import requests
from bs4 import BeautifulSoup
import os
url = "https://www.example.com" # 替换为需要爬取的网页地址
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
img_tags = soup.find_all("img")
folder_path = "D:/pic" # 新建文件夹路径
if not os.path.exists(folder_path):
os.makedirs(folder_path)
for img in img_tags:
img_url = img["src"]
img_name = img_url.split("/")[-1] # 获取图片名称
img_path = os.path.join(folder_path, img_name)
try:
img_response = requests.get(img_url)
with open(img_path, "wb") as f:
f.write(img_response.content)
except (requests.exceptions.RequestException, IOError):
print("捕获错误返回通信代码还有异常")
```
注意:在实际使用时,需要替换url和folder_path变量的值为实际需要爬取的网页地址和保存图片的文件夹路径。
阅读全文