import requests # 导入用于发送 HTTP 请求的 requests 库
from lxml import etree # 导入用于解析 HTML 的 etree 模块
import os # 导入用于处理文件路径的 os 模块
# 设置保存图片的目录
path = os.getcwd() + '/images04' # 获取当前工作目录,并创建一个名为 'images04' 的文件夹
if not os.path.exists(path): # 如果 'images04' 文件夹不存在,则创建它
os.mkdir(path)
# Bing 图片搜索的 URL 和请求头
url = 'https://cn.bing.com/images/search?q=一人之下图片&form=IQFRML&first=1&cw=1177&ch=693' # Bing 图片搜索的 URL
headers = { # 请求头,模拟浏览器发送请求
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0',
}
# 发送 HTTP 请求,获取搜索结果页面的 HTML 内容
response = requests.get(url, headers=headers) # 发送 GET 请求,获取页面内容
html = etree.HTML(response.text) # 使用 etree 模块解析 HTML 内容
# 通过 XPath 获取图片链接列表
images = html.xpath('//div[@class="dgControl waterfall"]//ul//li//a')
# 遍历图片链接列表
for image in images:
image_str = image.get('href') # 获取图片链接
image_url = 'https://cn.bing.com/' + image_str # 拼接完整的图片 URL
filename = image_str.split('/')[-1] # 提取图片文件名
image_response = requests.get(image_url, headers=headers) # 发送 GET 请求,获取图片内容
# 保存图片到本地文件
with open(filename, 'wb') as f: # 以二进制写入模式打开文件
f.write(image_response.content) # 将图片内容写入文件
出现下面的问题

