"""
爬取目标:https://pic.netbian.com/ 彼岸图
首页地址:https://pic.netbian.com/4kmeinv/
第N页:
https://pic.netbian.com/4kmeinv/index_N.html
https://pic.netbian.com/uploads/allimg/240709/194631-1720525591a682.jpg
仅供学习,不可用于违法用途
"""
import os.path
import requests
from bs4 import BeautifulSoup
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
}
url = "https://pic.netbian.com/4kmeinv/"
response = requests.get(url=url, headers=headers)
response.encoding = "gbk"
# print(response.text)
soup = BeautifulSoup(response.text, 'lxml')
# 获取所有图片
image_list = soup.select("ul.clearfix li a img")
print(image_list)
def download_image(src):
"""
下载图片
:param src:图片路径
:return:
"""
# 获取图片名称
file_name = os.path.basename(src)
print(file_name)
# 下载图片
try:
with open(f"./image/{file_name}", "wb") as file:
responses = requests.get("https://pic.netbian.com/" + src)
file.write(responses.content)
except:
print(src, "下载异常")
for image in image_list:
# print(image["src"])
download_image(image['src'])
以上代码内容是根据小峰博主视频教编写