数字时代,图片已经成为我们生活中的一部分。无论是社交媒体上的照片,还是网页中的图片元素,我们都希望能够方便地下载并进行个性化的处理。
假设你是一位设计师,你经常需要从网页上下载大量的图片素材,并为这些图片添加水印以保护你的作品。然而,手动下载和添加水印是一件繁琐的事情 ,这时就可以通过编写一个Python爬虫程序,自动化地完成这个任务,节省时间和精力。
我们的基本思路是通过发送HTTP请求获取网页内容,然后解析网页内容,提取出图片元素的URL。接下来,我们使用请求库下载这些图片,并使用Pillow库添加水印。最后,我们将处理后面的图片保存到本地。
在开始之前,我们需要准备以下工作:
pip install requests
pip install pillow
接下来,我们可以定义一个函数来发送HTTP请求并获取页面内容:
def fetch_page(url):
proxies = {
"http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
"https": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
}
response = requests.get(url, proxies=proxies)
return response.content
然后,我们可以编写一个函数来解析页面内容并提取图片元素的URL:
from bs4 import BeautifulSoup
def extract_image_urls(page_content):
soup = BeautifulSoup(page_content, "html.parser")
image_urls = []
for img in soup.find_all("img"):
image_urls.append(img["src"])
return image_urls
接下来,我们可以编写一个函数来下载图片并添加水印:
def download_and_add_watermark(image_url):
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
watermark = Image.new("RGBA", image.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(watermark)
font = ImageFont.truetype("arial.ttf", 36)
draw.text((10, 10), "Watermark", font=font, fill=(255, 255, 255, 128))
watermarked_image = Image.alpha_composite(image.convert("RGBA"), watermark)
watermarked_image.save("watermarked_image.png")
实现完整代码
以下是完整的Python代码:
import requests
from bs4 import BeautifulSoup
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
def fetch_page(url):
proxies = {
"http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
"https": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
}
response = requests.get(url, proxies=proxies)
return response.content
def extract_image_urls(page_content):
soup = BeautifulSoup(page_content, "html.parser")
image_urls = []
for img in soup.find_all("img"):
image_urls.append(img["src"])
return image_urls
def download_and_add_watermark(image_url):
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
watermark = Image.new("RGBA", image.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(watermark)
font = ImageFont.truetype("arial.ttf", 36)
draw.text((10, 10), "Watermark", font=font, fill=(255, 255, 255, 128))
watermarked_image = Image.alpha_composite(image.convert("RGBA"), watermark)
watermarked_image.save("watermarked_image.png")
def main():
# 获取网页的页面内容
page_content = fetch_page("https://www.58pic.com/")
# 提取图片元素的URL
image_urls = extract_image_urls(page_content)
# 下载图片并添加水印
for image_url in