每天一篇python:简单爬虫下载图片篇
来源:互联网 发布:第三方抽奖软件 编辑:程序博客网 时间:2024/06/05 22:56
使用python下载百度贴吧的图片,主要用到了urlib、和re 两个模块,具体代码如下:
#!/usr/bin/env python# -*- coding: utf-8 -*-'''Created on 2016年4月17日@author: damuou@title: 今天学习使用python 来爬取网页上的图片。'''#url相关import urllib#正则表达式相关import re#返回目标url的html源码 def getHtml(url): page=urllib.urlopen(url) html =page.read() return htmldef getImage(html): reg = r'src="(.+?\.jpg)" pic_ext' imgre = re.compile(reg) #读取html 中包含 imgre(正则表达式)的数据 imglist = re.findall(imgre,html) return imglist def downloadImage(urls): #此处注意for语法行以冒号结尾 x=1; for url in urls: 'urlretrieve解析:直接将远程数据下载到本地。' '''参数 finename 指定了保存本地路径(如果参数未指定,urllib会生成一个临时文件保存数据。)参数 reporthook 是一个回调函数,当连接上服务器、以及相应的数据块传输完毕时会触发该回调,我们可以利用这个回调函数来显示当前的下载进度。参数 data 指 post 到服务器的数据,该方法返回一个包含两个元素的(filename, headers)元组,filename 表示保存到本地的路径,header 表示服务器的响应头。''' urllib.urlretrieve(url,'%s.jpg' % x) x=x+1if __name__ == '__main__': html= getHtml("http://tieba.baidu.com/p/2460150866") print html imageUrls=getImage(html); print imageUrls downloadImage(imageUrls)
0 0
- 每天一篇python:简单爬虫下载图片篇
- python 简单爬虫下载图片
- 每天一篇python:文件下载篇
- python 爬虫下载图片
- Python简单图片爬虫
- Python简单图片爬虫
- python简单爬虫(下载知乎图片示例)
- Python 网络爬虫-下载图片
- python 爬虫下载网站图片
- python 爬虫批量下载图片
- python爬虫学习--下载图片
- 每天一篇python:数据类型篇
- 每天一篇python:函数篇
- 每天一篇python:对象篇
- 每天一篇python:系统篇
- Python简单爬虫爬取多页图片
- python中使用网络爬虫下载图片
- python 3.x网络爬虫 下载图片
- 在texlive下使用CJK的3条小心得
- iOS --- 理解Runtime机制及其使用场景
- mysql连接IP地址失败的原因
- 原型模式
- SVM核技巧的经典解释
- 每天一篇python:简单爬虫下载图片篇
- 常见linux命令
- 第7周项目2-友元类
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled.
- [LintCode] 前序遍历和中序遍历树构造二叉树 Construct Binary Tree from Preorder and Inorder Traversal
- AngularJS最理想开发工具WebStorm
- 制作图片数字 [代码清单10-3]
- leetcode_088 Merge Sorted Array
- 安卓学习笔记获取手机内存与SD卡路径及大小