Python-一键爬取图片、音频、视频资源

5,070 阅读2分钟

前言

使用Python爬取任意网页的资源文件,比如图片、音频、视频;一般常用的做法就是把网页的HTML请求下来通过XPath或者正则来获取自己想要的资源,这里我做了一个爬虫工具软件,可以一键爬取资源 媒体文件;但是需要说明的是,这里爬取资源文件只针对HTML已有的文件,如果需要二次请求的是爬取不到的,比如酷狗音乐播放界面,因为要做通用工具,匹配不同的网站!!!😀😀😀

这里主推图片爬取,一些需要图片素材的可以输入网址一键爬取!

还有就是爬取视频的时候会把磁力链接爬取下来!可以使用第三方下载工具下载!🤗

代码

爬取资源文件

这里需要说明的就只,有的图片资源并不是url链接,是data:image格式,这里需要转换一下存储!

def getResourceUrlList(url ,isImage, isAudio, isVideo):
	global imgType_list, audioType_list, videoType_list
	imageUrlList = []
	audioUrlList = []
	videoUrlList = []
 
	url = url.rstrip().rstrip('/')
	htmlStr = str(requestsDataBase(url))
	# print(htmlStr)
	
	Wopen = open('reptileHtml.txt','w')
	Wopen.write(htmlStr)
	Wopen.close()
 
	Ropen = open('reptileHtml.txt','r')
	imageUrlList = []
 
	for line in Ropen:
		line = line.replace("'", '"')
		segmenterStr = '"'
		if "'" in line:
			segmenterStr = "'"
 
		lineList = line.split(segmenterStr)
		for partLine in lineList:
			if isImage == True:
				# 查找图片
				if 'data:image' in partLine:
					base64List = partLine.split('base64,')
					imgData = base64.urlsafe_b64decode(base64List[-1] + '=' * (4 - len(base64List[-1]) % 4))
					base64ImgType = base64List[0].split('/')[-1].rstrip(';')
					imageName = zfjTools.getTimestamp() + '.' + base64ImgType
					imageUrlList.append(imageName + '$==$' + base64ImgType)
 
				# 查找图片
				for imageType in imgType_list:
					if imageType in partLine:
						imgUrl = partLine[:partLine.find(imageType) + len(imageType)].split(segmenterStr)[-1]
 
						# 修复URL
						imgUrl = repairUrl(imgUrl, url)
 
						sizeType = '_{' + 'size' + '}'
						if sizeType in imgUrl:
							imgUrl = imgUrl.replace(sizeType, '')
 
						imgUrl = imgUrl.strip()
 
						if imgUrl.startswith('http://') or imgUrl.startswith('https://') and imgUrl not in imageUrlList:
							imageUrlList.append(imgUrl)
						else:
							imgUrl = ''
 
			if isAudio == True:
				# 查找音频
				for audioType in audioType_list:
					if audioType in partLine or audioType.lower() in partLine:
						audioType = audioType.lower() if audioType.lower() in partLine else audioType
						audioUrl = partLine[:partLine.find(audioType) + len(audioType)].split(segmenterStr)[-1]
 
						# 修复URL
						audioUrl = repairUrl(audioUrl, url)
 
						if audioUrl.startswith('http://') or audioUrl.startswith('https://') and audioUrl not in audioUrlList:
							audioUrlList.append(audioUrl)
						else:
							audioUrl = ''
 
			if isVideo == True:
				# 查找视频
				for videoType in videoType_list:
					if videoType in partLine or videoType.lower() in partLine:
						videoType = videoType.lower() if videoType.lower() in partLine else videoType
						videoUrl = partLine[:partLine.find(videoType) + len(videoType)].split(segmenterStr)[-1]
 
						# 修复URL
						videoUrl = repairUrl(videoUrl, url)
 
						if videoUrl.startswith('http://') or videoUrl.startswith('https://') or videoUrl.startswith('ed2k://') or videoUrl.startswith('magnet:?') or videoUrl.startswith('ftp://') and videoUrl not in videoUrlList:
							videoUrlList.append(videoUrl)
						else:
							videoUrl = ''
 
	return (imageUrlList, audioUrlList, videoUrlList)

爬取自定义节点

# 统配节点爬取
def getNoteInfors(url, fatherNode, childNode):
	url = url.rstrip().rstrip('/')
	htmlStr = requestsDataBase(url)
	
	Wopen = open('reptileHtml.txt','w')
	Wopen.write(htmlStr)
	Wopen.close()

	html_etree = etree.HTML(htmlStr)

	dataArray = []

	if html_etree != None:
		nodes_list = html_etree.xpath(fatherNode)
		for k_value in nodes_list:
			partValue = k_value.xpath(childNode)
			if len(partValue) > 0:
				dataArray.append(partValue[0])

	return dataArray

软件

软件下载地址gitee.com/zfj1128/ZFJ…

使用教学视频

资源爬取:链接:pan.baidu.com/s/1xa9ruF_h… 密码:1zpg

节点爬取:链接:pan.baidu.com/s/1ebWWYtjo… 密码:cosa

使用截图如下:

WX20190802-162443@2x.png

结束语

欢迎各位大佬提出宝贵的意见和建议!!!!🤗🤗🤗