加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python实现爬图,不要再爬妹子图了,太没品了

发布时间:2020-12-17 17:20:34 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # !/usr/bin python#--*-- coding:utf-8 --*--'''批量下载任意网址上的图片linux下的路径,有需要的改改路径,很简单,做为学习urllib模块的一个简单

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

# !/usr/bin python
#--*-- coding:utf-8 --*--

'''
批量下载任意网址上的图片
linux下的路径,有需要的改改路径,很简单,做为学习urllib模块的一个简单范例吧
'''
import os
import urllib
import re

url = ""

def getHtml(url):
	file = urllib.urlopen(url)
	html = file.read()
	return html

def getImageList(html):
	reg = 'http[^"]*?.jpg'
	imgre = re.compile(reg)
	imgList = re.findall(imgre,html)
	return imgList

def printImageList(imgList):
	for i in imgList:
		print i

def download(imgList,page):
	x = 1
	for imgurl in imgList:
		print 'download file '+str(x)+' start'
		urllib.urlretrieve(imgurl,'./webImage/%s_%s.jpg'%(page,x))
		print 'download file '+ str(x)+ ' end'
		x+=1


def downImageNum(pagenum):
	page = 1
	pageNumber = pagenum
	while(page <= pageNumber):
		html = getHtml(url)#获得url指向的html内容
		imageList = getImageList(html)#获得所有图片的地址,返回列表
		printImageList(imageList)#打印所有的图片的地址
		download(imageList,page)#下载所有的图片
		page = page+1

if __name__ == '__main__':
	os.system('mkdir webImage')
	url = raw_input("enter the web page:")
	downImageNum(1)



以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读