[python爬虫]如何爬取特定网页的图片
2015-08-02 23:47
661 查看
#!/usr/bin/env python # -*- coding:utf-8 -*- """ Created on Sun Aug 02 20:10:36 2015 @author: lijiong """ import urllib import sys import chardet import re def 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)
相关文章推荐
- [2015-08-02] python014
- [python]findall函数讲解
- Python学习之类
- python常用函数
- python科学计算_numpy_ufunc
- python菜鸟日记2
- Python Socket实现简单的聊天室
- php与python局部变量区别
- python---解决“Unable to find vcvarsall.bat”错误
- Python select模块学习
- python utf8
- Python的机器学习库汇总与梳理
- python机器学习库scikit-learn
- Python学习之生成器
- Python IDE:PyCharm中的那些实用功能
- PyCharm安装Python插件
- Python学习之装饰器
- python find命令、startwith命令
- Python学习之文件
- Python学习之异常