基于python的urllib 库抓取网站上的图片

最近写了个爬虫实例,有python环境的话就可以直接运行了。

运行效果是这样的:

完整代码如下:

import urllib

import urllib.request

import re

import random

import time

import os

#目标网址:

imagePath="https://pic.netbian.com"

#用户代理池

uapools=[

"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.31",

"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",

]

#//创建保存图片的目录

imageSavePath="D:\\myimg\\"

if not os.path.isdir(imageSavePath):

os.mkdir(imageSavePath)

def UA():

opener=urllib.request.build_opener()

thisua=random.choice(uapools)

ua=("User-Agent",thisua)

opener.addheaders=ua

urllib.request.install_opener(opener)

print("当前使用ua:"+str(thisua))

UA()

thisurl="https://pic.netbian.com/4kqiche?s=98575646"

data=urllib.request.urlopen(thisurl).read().decode("gbk","ignore")

pat=re.compile('<img src="(/uploads/.*?)".alt="(.*?)"./>')

rst=re.findall(pat,data)

for j in rst:

link=j0

name=j1

imageUrl=''.join(imagePath+link)

res=urllib.request.urlretrieve(imageUrl,imageSavePath+"\\"+name+".jpg")

print(name+".jpg 获取成功....")

相关推荐
cup114 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi006 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵8 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf9 小时前
Agent 流程编排
后端·python·agent
copyer_xyf9 小时前
Agent RAG
后端·python·agent
copyer_xyf9 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf10 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏