获取百度卡通图片的爬虫
对于获取网站上的图片,最常用的方式是使用Python中的requests和BeautifulSoup库,以下是一个简单的爬虫示例,可以获取百度卡通图片:
import requests
from bs4 import BeautifulSoup
import os
# 设置请求头
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
# 设置要爬取的网址和存储路径
url = 'https://image.baidu.com/search/index?tn=baiduimage&word=%E7%99%BE%E5%BA%A6%E5%8D%A1%E9%80%9A'
save_path = './images/'
# 创建存储路径
if not os.path.exists(save_path):
os.makedirs(save_path)
# 发送请求
response = requests.get(url, headers=headers)
# 解析网页内容
soup = BeautifulSoup(response.content, 'html.parser')
# 获取所有图片链接
img_links = soup.find_all('img')
# 遍历所有链接,下载图片
for img in img_links:
try:
img_url = img['src']
# 如果链接以http开头,则不需要加上前缀
if img_url.startswith('http'):
pass
else:
img_url = 'http:' + img_url
# 获取图片名称
img_name = img_url.split('/')[-1]
# 发送请求,下载图片
img_data = requests.get(img_url).content
with open(save_path + img_name, 'wb') as f:
f.write(img_data)
print('下载图片:', img_name)
except:
print('下载失败:', img_url)
在执行爬虫之前,需要先安装requests和BeautifulSoup库:
pip install requests
pip install beautifulsoup4
在执行时,会自动创建一个名为images的文件夹,并将爬取到的图片保存在其中
原文地址: http://www.cveoy.top/t/topic/huFd 著作权归作者所有。请勿转载和采集!