本文主要是介绍爬取一人之下所有图片的uzr以及源代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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_urlprint(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:
保存到本地:
这篇关于爬取一人之下所有图片的uzr以及源代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!