[python爬虫]如何爬取特定网页的图片

来源:互联网 发布:苹果 office 类似软件 编辑:程序博客网 时间:2024/05/22 11:56

#!/usr/bin/env python# -*- coding:utf-8 -*-"""Created on Sun Aug 02 20:10:36 2015@author: lijiong"""import urllibimport sysimport chardetimport redef get_html(url):    page = urllib.urlopen(url)    content = page.read()    typeEncode = sys.getfilesystemencoding()    infoencode = chardet.detect(content).get('encoding','utf-8')    html = content.decode(infoencode,'ignore').encode(typeEncode)    return html      #提取html页面,编码已经转换def get_img(html):    reg = r'src="(http://imgsrc.baidu.com/forum/w.*?\.jpg)"'     imgre = re.compile(reg)    imglist = re.findall(imgre, html) #表达式中只有一个括号时,findall只会返回括号的内容    i = 0    for imgurl in imglist:        print imgurl        urllib.urlretrieve(imgurl, 'C:/Users/lijiong/Desktop/new/%s.jpg'%i)        i+=1        html = get_html('http://tieba.baidu.com/p/3837885162')get_img(html)


0 0
原创粉丝点击