# -*- coding: utf-8 -*-
import requests
import re
import sys
reload(sys)
sys.setdefaultencoding('utf-8') if __name__ == '__main__':
url = 'http://photostock.china.com.cn/Web_CHN/SpecialTopicPhoto.aspx?Id=296'
html = requests.get(url)
img_src = re.findall('<img alt=.*?src="..(.*?)".*?/>', html.text, re.S)
imgUrl = []
for each_src in img_src:
imgUrl.append("http://photostock.china.com.cn" + each_src)
picName = 100
for each in imgUrl:
imgContext = requests.get(each).content
with open("lovelyAnimals/" + str(picName) + ".jpg", "wb") as code:
code.write(imgContext)
picName += 1 '''
下载文件的3种方法
(1): 使用urllib.urlretrieve方法,可在callbackfunc函数中显示下载进度
def callbackfunc(blocknum, blocksize, totalsize):
# 回调函数
# @blocknum:
# 已经下载的数据块 # @blocksize:
# 数据块的大小 # @totalsize:
# 远程文件的大小
percent = 100.0 * blocknum * blocksize / totalsize
if percent > 100:
percent = 100
print "%.2f%%"% percent
url = 'http://www.sina.com.cn'
local = 'lovelyAnimals/sina.html'
urllib.urlretrieve(url, local, callbackfunc) (2):使用urllib2.urlopen
import urllib2
url = 'http://www.sina.com.cn'
f = urllib2.urlopen(url)
data = f.read()
with open("lovelyAnimals/sina.html", "wb") as code:
code.write(data) (3):使用requests模块
import requests
url = 'http://www.sina.com.cn'
html = requests.get(url)
with open("lovelyAnimals/sina.html", "wb") as code:
code.write(html.content)
'''

最新文章

  1. 转载:log4j.properties log4j.xml 路径问题
  2. 解析json格式数据
  3. jexus ASP.NET开发注意项1
  4. Mysql 5.7.7
  5. Android 进阶 Fragment 介绍和使用 (一)
  6. HDU 1072 (不一样的入队条件) Nightmare
  7. 【shell】if
  8. css.day03
  9. Nodejs随笔(一):Hello World!
  10. Qt编写端口扫描工具
  11. 【原创】纯OO:从设计到编码写一个FlappyBird (六)
  12. 一次MySQL死锁问题解决
  13. eclipse和myeclipse中将项目发布到tomcat的root目录
  14. Day2 Python基础之基本操作(一)
  15. 列表去重几种方法 python
  16. 完全揭秘log file sync等待事件-转自itpub
  17. jquery 取子节点及当前节点属性值
  18. poj 2125 Destroying The Graph (最小点权覆盖)
  19. 如何提取kinect中深度图的点云数据
  20. Codeforces 950.E Data Center Maintenance

热门文章

  1. Mysql的内存优化
  2. MEME(Motif-based sequence analysis tools)使用说明
  3. 教你用PS修复老照片
  4. Java数据结构和算法(三)顺序存储的树结构
  5. Plugin &#39;Scala&#39; is incompatible with this.installation
  6. Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子
  7. 2018.08.10 atcoder Median Sum(01背包)
  8. 关于调用Feign client超时得不到结果的问题
  9. IntelliJ IDEA 2017版 spring-boot修改端口号配置把端口号改为8081
  10. spark 写 hbase 数据库,遇到Will not attempt to authenticate using SASL (unknown error)