Python爬虫---------------图片抓取(1)

时间:2022-11-16 16:47:57

先上个人项目: https://github.com/wanglingxxx


抓取了某页的所有图片 保存在本地


Code:

# -*- coding:utf8 -*-
'''
@author: ChenPeng
'''
import urllib, urllib2 ,os, re
# 创建文件夹,昨天刚学会
path = os.getcwd() # 获取此脚本所在目录
new_path = os.path.join(path,u'妹子图')
if not os.path.isdir(new_path):
os.mkdir(new_path)

def OnlyCharNum(s,oth=''):
s2 = s.lower();
fomart = 'abcdefghijklmnopqrstuvwxyz0123456789'
for c in s2:
if not c in fomart:
s = s.replace(c,'');
return s;

def getImg(html):
reg = r'src="(.+?\.jpg)" '
imgre = re.compile(reg)
imglist = re.findall(imgre,html)
x = 0
for imgurl in imglist:
imgName = OnlyCharNum(imgurl)
imgName = imgName + '.jpg'
content2 = urllib2.urlopen(imgurl).read()
with open(u'妹子图'+'/'+imgName,'wb') as code:
code.write(content2)

print ' all image done'
return imglist

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

html = getHtml("http://www.meizitu.com/a/5478.html")

print getImg(html)


效果图:


Python爬虫---------------图片抓取(1)


最后,感谢支持。