python实现爬图,不要再爬妹子图了,太没品了
发布时间:2020-05-25 00:10:35 所属栏目:Python 来源:互联网
导读:python实现爬图,不要再爬妹子图了,太没品了
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 # !/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)
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
