基于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=j[0]

name=j[1]

imageUrl=''.join(imagePath+link)

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

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

相关推荐
豐儀麟阁贵几秒前
8.2异常的抛出与捕捉
java·开发语言·python
interception23 分钟前
爬虫js逆向,jsdom补环境,抖音,a_bogus
javascript·爬虫·python
林炳然30 分钟前
Python-Basic Day-5 函数-生成器&装饰器
python
Yue丶越43 分钟前
【Python】基础语法入门(四)
linux·开发语言·python
AI街潜水的八角4 小时前
Python电脑屏幕&摄像头录制软件(提供源代码)
开发语言·python
hadage2334 小时前
--- git 的一些使用 ---
开发语言·git·python
笨笨聊运维10 小时前
CentOS官方不维护版本,配置python升级方法,无损版
linux·python·centos
Gerardisite10 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php