有些网站的登录方式是验证码登录的方式,比如今天我们要测试的网站专利检索及分析。

http://www.pss-system.gov.cn/sipopublicsearch/portal/uilogin-forwardLogin.shtml

登录此类网站的关键是识别其中的验证码。那么如何识别验证码呢。我们首先来看下网页源代码。在网页中,验证码的是通过下载一个图片得到的。图片的下载地址是src=/sipopublicsearch/portal/login-showPic.shtml

我们从实际的fiddler抓包来看,也是通过请求上面的图片源地址得到了JPEG的图片并显示在浏览器上

那么在scrapy中我们首先就要将图片下载到本地,然后进行识别
def parse(self,response):
    ret=response.xpath('//*[@id="codePic"]/@src').extract()
    image_source=ret[0]
    image_url=response.urljoin(image_source)
    r=requests.get(image_url)
    with open('E://scrapy_project/image2.JPEG',"wb") as code:
        code.write(r.content)
首先提取src的值出来,然后使用requests的方法进行图片下载并保存。打开文件如下。

下一步就是开始识别图片中的验证码了,这就需要用到pytesser以及PIL库了。
首先是安装Tesseract-OCR,在网上下载后进行安装。默认安装路径是C:\Program Files\Tesseract-OCR。将该路径添加到 系统属性的path路径里面。
然后再通过pip安装pytesseract以及PIL。下面来看下如何使用。代码如下:
im=Image.open('E:\\scrapy_project\\image2.JPEG')
im.convert('L')
ret=image_to_string(im,config='-psm 7’)
print ret
结果如下:图片中的验证码已经被识别出来了
image_to_string要配置psm N,参数解释如下,一般我们选择第7个
-psm N
    Set Tesseract to only run a subset of layout analysis and assume a certain form of image. The options for N are:
 
    0 = Orientation and script detection (OSD) only.
    1 = Automatic page segmentation with OSD.
    2 = Automatic page segmentation, but no OSD, or OCR.
    3 = Fully automatic page segmentation, but no OSD. (Default)
    4 = Assume a single column of text of variable sizes.
    5 = Assume a single uniform block of vertically aligned text.
    6 = Assume a single uniform block of text.
    7 = Treat the image as a single text line.
    8 = Treat the image as a single word.
    9 = Treat the image as a single word in a circle.
    10 = Treat the image as a single character.
E:\python2.7.11\python.exe E:/py_prj/test3.py
8227

												

python网络爬虫之如何识别验证码的更多相关文章

  1. Python网络爬虫学习总结

    1.检查robots.txt 让爬虫了解爬取该网站时存在哪些限制. 最小化爬虫被封禁的可能,而且还能发现和网站结构相关的线索. 2.检查网站地图(robots.txt文件中发现的Sitemap文件) ...

  2. 《实战Python网络爬虫》- 感想

    端午节假期过了,之前一直在做出行准备,后面旅游完又休息了一下,最近才恢复状态. 端午假期最后一天收到一个快递,回去打开,发现是微信抽奖中的一本书,黄永祥的<实战Python网络爬虫>. 去 ...

  3. Python 网络爬虫干货总结

    Python 网络爬虫干货总结 爬取 对于爬取来说,我们需要学会使用不同的方法来应对不同情景下的数据抓取任务. 爬取的目标绝大多数情况下要么是网页,要么是 App,所以这里就分为这两个大类别来进行了介 ...

  4. Python网络爬虫与信息提取

    1.Requests库入门 Requests安装 用管理员身份打开命令提示符: pip install requests 测试:打开IDLE: >>> import requests ...

  5. 《精通Python网络爬虫》|百度网盘免费下载|Python爬虫实战

    <精通Python网络爬虫>|百度网盘免费下载|Python爬虫实战 提取码:7wr5 内容简介 为什么写这本书 网络爬虫其实很早就出现了,最开始网络爬虫主要应用在各种搜索引擎中.在搜索引 ...

  6. 《精通python网络爬虫》笔记

    <精通python网络爬虫>韦玮 著 目录结构 第一章 什么是网络爬虫 第二章 爬虫技能概览 第三章 爬虫实现原理与实现技术 第四章 Urllib库与URLError异常处理 第五章 正则 ...

  7. Python网络爬虫与信息提取笔记

    直接复制粘贴笔记发现有问题 文档下载地址//download.csdn.net/download/hide_on_rush/12266493 掌握定向网络数据爬取和网页解析的基本能力常用的 Pytho ...

  8. 关于Python网络爬虫实战笔记③

    Python网络爬虫实战笔记③如何下载韩寒博客文章 Python网络爬虫实战笔记③如何下载韩寒博客文章 target:下载全部的文章 1. 博客列表页面规则 也就是, http://blog.sina ...

  9. 关于Python网络爬虫实战笔记①

    python网络爬虫项目实战笔记①如何下载韩寒的博客文章 python网络爬虫项目实战笔记①如何下载韩寒的博客文章 1. 打开韩寒博客列表页面 http://blog.sina.com.cn/s/ar ...

随机推荐

  1. C# 导出 数据 到Excel

    /// <summary> /// 实现将数据导出至Excel, /// 在上面的代码中,我们首先将gridview绑定到指定的数据源中,然后在button1的按钮(用来做导出到EXCEL ...

  2. mac python 切换系统默认版本

    1 找到所安装python路径/usr/local/Cellar/python/2.7.13/bin2 vim ~/.bash_profile 3 添加如下代码: PATH="/usr/lo ...

  3. 【GLSL教程】(九)其他说明 【转】

    http://blog.csdn.net/racehorse/article/details/6664775 法线矩阵 在很多顶点shader中都用到了gl_NormalMatrix.这里将介绍这个矩 ...

  4. 转: CSRF(Cross Site Request Forgery 跨站域请求伪造) 背景与介绍

    from:  https://www.ibm.com/developerworks/cn/web/1102_niugang_csrf/   在 IBM Bluemix 云平台上开发并部署您的下一个应用 ...

  5. Python setup.py和MANIFEST.in文件

    Setup.py文件 from setuptools import setup from codecs import open # 第三方依赖包及版本号 requires = ['beautifuls ...

  6. PHP执行linux系统命令

    本文是第一篇,讲述如何在PHP中执行系统命令从而实现一些特殊的目的,比如监控服务器负载,重启MySQL.更新SVN.重启Apache等.第二篇<PHP监控linux服务器负载>:http: ...

  7. vuex mapGetters

    1.vuex 配置 //vuex的配置 //注意Store是大写 const store = new Vuex.Store({ //数据保存 state: { show: false, count: ...

  8. 使用rabbitmq rpc 模式

        服务器端     安装 ubuntu 16.04 server     安装 rabbitmq-server     设置 apt 源 curl -s https://packagecloud ...

  9. 16进制颜色转换为UIColor

    objc #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >& ...

  10. 关于iOS Tabbar的一些设置

    事实上iOS Tabbar的可定制性很高,我们没有必要反复造轮子,以下是笔者收集的一些tabbar的经常使用设置.希望对大家有所帮助. 设置tabbar选中颜色 iOS7设置例如以下: [self.t ...