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)

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

Python_summary的更多相关文章

随机推荐

  1. [No0000182]Parallel Programming with .NET-Partitioning in PLINQ

    Every PLINQ query that can be parallelized starts with the same step: partitioning.  Some queries ma ...

  2. [No0000117]visual studio 调试WebForm 显示 HTTP Error 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。

    调试界面如下: 解决办法1:右键设置起始页. 影响文件: 解决方案2:Web.config中添加默认页面配置: <system.webServer> <defaultDocument ...

  3. [No0000121]Python教程4/9-输入和输出

    输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, world') p ...

  4. winform excel导入--自带office.interop.excel方式

    用npoi方式,遇到一个问题,有的excel用加密软件(盖章,只读等)生成的解析不了,所以换成自带的方式,可以解决. 需要引用系统自带Microsoft.office.interop.excel pu ...

  5. [skill][gdb][coredump][abrt] 使用abrt管理程序coredump

    abrt:Automatic bug detection and reporting tool https://github.com/abrt/abrt 常用的命令: abrt-auto-report ...

  6. odoo中Python实现小写金额转换为大写金额

    自动将小写的金额数值转换为大写,方便记录 class project_parm(models.Model): def IIf(self, b, s1, s2): if b: return s1 els ...

  7. python运算符,数据类型,数据类型操作,三目运算,深浅拷贝

    算数运算符: Py2中精确除法需要导入:from __future__ import division,(符由特  ,将来的.滴未省,除法) py3不需要导入 赋值运算符: 比较运算符: 成员运算符: ...

  8. 使用Python + Selenium破解滑块验证码

    在前面一篇博客<使用 Python + Selenium 打造浏览器爬虫>中,我介绍了 Selenium 的基本用法和爬虫开发过程中经常使用的一些小技巧,利用这些写出一个浏览器爬虫已经完全 ...

  9. es修改指定的field(partial update)

    PUT /index/type/id 创建文档&替换文档,就是一样的语法一般对应到应用程序中,每次的执行流程基本是这样的:1.应用程序发起一个get请求,获取到document,展示到前台界面 ...

  10. Ubuntu上qt环境的构建

    写在前面.......这个教程好像比较早一点了,现在介绍一个新的思路: 整体参见如下步骤(for zedboard): 1.首先下载qt-opensource-linux.run文件,然后跟在Wind ...