Python爬虫下载网站上的图片到本地
2016-06-03 18:38
721 查看
需求:
想把某个网站上的所有图片(比如一些妹子图)下载到本地
工具:
使用Python 3 语言,开发工具为下载python时自带的IDLE
(需要注意,python3和python2的命令有区别的!查看或者编写代码的时候一定要看python版本)
命令:
CMD命令行:python D:/picture/
(改成自己的文件夹,图片将会下载到此目录)
代码:
#coding:utf-8 import requests from bs4 import BeautifulSoup import re DownPath = "D:/python/getData/picture/" import urllib head = {'User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'} TimeOut = 5 PhotoName = 0 c = '.jpeg' PWD="D:/python/getData/picture/" for x in range(1,4): site = "http://www.meizitu.com/a/qingchun_3_%d.html" %x Page = requests.session().get(site,headers=head,timeout=TimeOut) Coding = (Page.encoding) Content = Page.content.decode(Coding).encode('utf-8') ContentSoup = BeautifulSoup(Content) jpg = ContentSoup.find_all("img")#find_all("HTML的标签") #print(jpg) 会出现错误,不是因为获取错了,是不能显示出来 for photo in jpg: PhotoAdd = photo.get('src')#get('标签里的属性') print(PhotoAdd) PhotoName +=1 Name = (str(PhotoName)+c) r = requests.get(PhotoAdd,stream=True) with open(PWD+Name, 'wb') as fd: for chunk in r.iter_content(): fd.write(chunk) print ("You have down %d photos" %PhotoName)
相关文章推荐
- Python的Django中将文件上传至七牛云存储的代码分享
- python开源机器学习工具scikit中文教程
- Python学习笔记 —— 运算符
- Python
- 基于 Python 和 Scikit-Learn 的机器学习介绍
- python中decorator
- My Library
- Python - 练习 判断输入是奇数,偶数,小数,还是字符
- Python实现遍历url
- 【Leetcode】287. Find the Duplicate Number My Submissions QuestionEditorial Solution python实现
- Python subprocess模块
- 详解duck typing鸭子类型程序设计与Python的实现示例
- Python数据类型之间的转换
- Eclipse和PyDev搭建完美Python开发环境(Windows篇)
- 在mac上搭建python环境
- python 2.7 访问 MS sql
- python环境搭建和安装软件管理器pip
- Python条件语句if…else…
- 详解字典树Trie结构及其Python代码实现
- Python中利用Scipy包的SIFT方法进行图片识别的实例教程