第一版:
import requests
import os
from bs4 import BeautifulSoup
# 设置网页地址
url = "https://www.Baidu.com"
# 发送请求并获取页面内容
response = requests.get(url)
content = response.content
# 使用 BeautifulSoup 解析页面
soup = BeautifulSoup(content, 'html.parser')
# 设置下载文件夹路径
download_folder = "./图片检索下载文件夹"
# 创建下载文件夹
if not os.path.exists(download_folder):
os.makedirs(download_folder)
# 查找页面中的图片元素
images = soup.find_all("img")
# 下载图片
for image in images:
src = image["src"]
response = requests.get(src)
if response.status_code == 200:
with open(os.path.join(download_folder, os.path.basename(src)), "wb") as f:
f.write(response.content)
第二版:
import requests
import os
from bs4 import BeautifulSoup
def download_images(url, download_folder):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
images = soup.find_all('img')
if not os.path.exists(download_folder):
os.makedirs(download_folder)
for i, image in enumerate(images):
img_url = image.get('src')
response = requests.get(img_url, stream=True)
if response.status_code == 200:
with open(os.path.join(download_folder, str(i)+'.jpg'), 'wb') as f:
for chunk in response:
f.write(chunk)
else:
print(f"Skipping {img_url} due to status code {response.status_code}")
url = 'https://w.wallhaven.cc/full/9d/wallhaven-9ddx9x.jpg'
download_folder = '图片检索下载文件夹'
download_images(url, download_folder)
第三版:
import requests
import os
from bs4 import BeautifulSoup
# 设置网站地址
url = input("请在这里输入你要检索的地址: ")
# 设置下载位置
folder_location = r'.\图片检索下载文件夹'
if not os.path.exists(folder_location):
os.makedirs(folder_location)
# 请求网页
response = requests.get(url)
content = response.content
# 解析网页
soup = BeautifulSoup(content, "html.parser")
for img in soup.findAll("img"):
img_url = img.attrs.get("src")
if not img_url:
continue
if "http" not in img_url:
# 如果图片的 URL 不是完整的,则拼接完整的 URL
img_url = url + img_url
response = requests.get(img_url)
try:
if response.status_code == 200:
# 根据图片 URL 设置文件名
file_name = os.path.join(folder_location, img_url.split("/")[-1])
with open(file_name, "wb") as f:
f.write(response.content)
except Exception as e:
print("下载错误!")
print("所有图片已下载!")
本文来自投稿,不代表本站立场,如若转载,请注明出处:https://www.4kit.cn/post-12.html
发表评论