How to scrape images from a specific website using Python?
To scrape images from a website, you can use the requests and BeautifulSoup libraries in Python. Below is a simple example code:
import requests
from bs4 import BeautifulSoup
import os
# 要爬取的网站URL
url = "https://www.example.com"
# 发送GET请求获取网页内容
response = requests.get(url)
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(response.content, "html.parser")
# 创建一个保存图片的文件夹
os.makedirs("images", exist_ok=True)
# 找到所有的图片标签
img_tags = soup.find_all("img")
# 逐个下载图片并保存到文件夹中
for img_tag in img_tags:
# 获取图片的URL
img_url = img_tag["src"]
# 发送GET请求下载图片
img_response = requests.get(img_url)
# 提取图片文件名
img_filename = img_url.split("/")[-1]
# 保存图片到文件夹中
with open("images/" + img_filename, "wb") as img_file:
img_file.write(img_response.content)
print("下载图片", img_filename, "成功!")
The code above will download all images from a specified website and save them in a folder named “images” in the current directory. You just need to replace “https://www.example.com” in the code with the URL of the website you want to crawl. After running the code, all images will be automatically downloaded and saved in the “images” folder.