初学者一枚,代码都是模仿网上的。亲测可用~
运行脚本的前提是本机安装了httplib2模块
#!/usr/bin/python
import os
import re
import string
import urllib
#author:reed
#date:2014-05-14
def GetWebPictures():
url=raw_input('please input the website you want to download:')
imgcontent=urllib.urlopen(url).read()
urllist=re.findall(r'src="(http.+?\.jpg)"',imgcontent,re.I)
if not urllist:
print 'not found...'
else:
filepath=os.getcwd()+'\img1'
if os.path.exists(filepath) is False:
os.mkdir(filepath)
x=1
print 'begin to get pictures...'
for imgurl in urllist:
temp=filepath+'\%s.jpg' % x
print 'it is downloading the %s pictures'% x
print imgurl
urllib.urlretrieve(imgurl,temp)
x+=1
print 'download completely...is saved at '+filepath
GetWebPictures()
这是一个简单的Python脚本,用于从指定网站抓取并下载所有JPEG图片。用户输入网址后,脚本通过正则表达式匹配图片src属性,并使用urllib模块下载到当前目录的img1文件夹中。
1万+

被折叠的 条评论
为什么被折叠?



