Python_summary

Q: python中出现IndentationError:unindent does not match any outer indentation level
A:复制代码的时候容易出现缩进错误,虽然看起来是缩进了,但是实际上没有。可以用Notepad++下的
     视图->显示符号->显示空格和制表符 来观察是否缩进

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~简单爬虫python2.7~~~~~~~~~~~~~~~~~~~~~~~~~~

'''
简单爬虫
'''
#encoding:utf-8

import urllib
import sys
import re

#设置编码
reload(sys)
sys.setdefaultencoding('utf-8')
#获取系统编码格式
type = sys.getfilesystemencoding()
def getHtml(url):
	page = urllib.urlopen(url)
	html = page.read().decode('utf-8').encode(type)
	return html

def cbk(a,b,c):
	'''
	a:已经下载的数据块
	b:数据块的大小
	c:远程文件的大小
	'''
	per = 100.0*a*b/c
	if per > 100 :
		per = 100
	print '%.2f%%' %per	
	
def getImg(html):
	reg = r'src="(.+?\.jpg)" alt'	
	imgre = re.compile(reg)
	imglist = re.findall(imgre,html)
	#x = 0
	for img in imglist:
		local = 'c://Users/xujianjun/Desktop/python/x.jpg' #不能只包含路径,必须是路径+文件名
		urllib.urlretrieve(img,local,cbk)  #回调函数定义必须有三个参数,哪怕不需要
		#x += 1
	return imglist
html = getHtml("http://www.cnblogs.com/1023linlin/p/8525273.html")	
print getImg(html)

  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

转载于:https://www.cnblogs.com/1023linlin/p/8648499.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值