将网址保存到join中,图片源代码保存到本地目录中
import requests
from lxml import etree
import json
import os
from urllib import request
# 设置Bing搜索URL和请求头
url = 'https://cn.bing.com/images/search?q=%E4%B8%80%E4%BA%BA%E4%B9%8B%E4%B8%8B%E5%9B%BE%E7%89%87&form=IQFRML&first=1&cw=1177&ch=693'
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',
}
# 发送GET请求获取Bing搜索结果页面的HTML内容
response = requests.get(url, headers=headers)
text = response.text
html = etree.HTML(text)
# 使用XPath选择器提取图片链接元素
images = html.xpath('//div[@class="dgControl waterfall"]/ul//a[@class="iusc"]')
# 创建一个列表来存储所有的图片URL
image_list = []
# 遍历图片链接元素,提取图片URL,并添加到列表中
for image in images:
image_url = image.get('href')
# 在图片URL前面加上前缀
full_image_url = "https://cn.bing.com/" + image_url
print(full_image_url)
request.urlretrieve(full_image_url, '一人之下')
image_list.append(full_image_url)
# 检查文件是否已存在,如果存在则追加,否则创建新文件
file_path = 'image.json'
if os.path.exists(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
existing_data = json.load(f)
image_list.extend(existing_data)
# 写入图片URL到JSON文件中,并增加换行和缩进,提高可读性
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(image_list, f, ensure_ascii=False, indent=4)
运行结果:
join:
本地目录: