这是一个稍微复杂的demo,它的功能如下:

  1. 输入专利号,下载对应的专利文档
  2. 输入关键词,下载所有相关的专利文档

0. 模块准备

  • 首先是requests,这个就不说了,爬虫利器
  • 其次是安装tesseract-ocr,pytesseract 和 PIL 「用于识别验证码」

1. 模拟登陆

我们需要对 这个网站 专利检索及分析 进行分析,反复鼓捣之后发现,找不到下载链接?

tell my why? 原来是没有登陆。 果然,登陆之后能够找到下载按钮,并手动下载成功。

注意到,在登陆和下载的同时,还需要输入验证码。

这样,我们第一步要解决的问题,便是验证码识别与模拟登陆

apt-get install tesseract-ocr
pip install pytesseract
pip install PIL

2. 关键字检索请求

登陆之后,我们键入一个关键词,进行检索

浏览器会向上图所示的url发送post请求,form data中的searchExp是我们的关键词

那么,我们通过post请求,便可以得到检索结果了

3. 翻页

把鼠标放在下一页上,我们能看到,是调用js来实现页面跳转的。

进一步分析,实际上我们对上图的url进行post请求也能实现翻页。

注意到form data中的参数

"resultPagination.limit":"10",
"resultPagination.sumLimit":"10",
"resultPagination.start":cnt, #这次参数决定了当前页面从搜索到的第几个数据开始
"resultPagination.totalCount":total, #总的搜索数据数目
"searchCondition.searchType":"Sino_foreign",
"searchCondition.dbId":"",
"searchCondition.extendInfo['MODE']":"MODE_GENERAL",
"searchCondition.searchExp":keywords, #我们输入的关键词
"wee.bizlog.modulelevel":"0200101",
"searchCondition.executableSearchExp":executableSearchExp, #需要我们自己构造
"searchCondition.literatureSF":literatureSF, #需要我们自己构造
"searchCondition.strategy":"",
"searchCondition.searchKeywords":"",
"searchCondition.searchKeywords":keywords # 我们输入的关键词

所以我们需要翻页的时候向 showSearchResult-startWa.shtml 这个页面进行post请求即可,

注意每次更新resultPagination.start参数

4. 浏览文档

需要下载专利文献,我们需要跳转到另外一个页面,而这个页面,又是通过js跳转的。

注意这里跳转的同时,传入的参数即为专利号,我们把它们保存下了,后面的post请求需要用到

可以看到,通过js跳转到了一个新窗口showViewList

这里的viewQC.viewLiteraQCList[0].searchCondition.executableSearchExp:非常关键,

这正是之前将它们保存下来的原因。

'viewQC.viewLiteraQCList[0].srcCnName':cnName,
'viewQC.viewLiteraQCList[0].srcEnName':srcEnName,
'viewQC.viewLiteraQCList[0].searchStrategy':'',
'viewQC.viewLiteraQCList[0].searchCondition.executableSearchExp':condition,
'viewQC.viewLiteraQCList[0].searchCondition.sortFields':'-APD,+PD',
'viewQC.needSearch':'true',
'viewQC.type':'SEARCH',
'wee.bizlog.modulelevel':'0200604'

5. 下载文档

点击下载按钮,弹出一个新的界面,Nextwork中产生三个请求

第三个便是验证码对应的图片

手动输入验证码后,首先通过validateMask.shtml进行验证码校验,并返回一个加密过后的mask串

再向downloadLitera.do发送post请求,完成下载!其中的参数,需要在之前自行确定。

6. 初步代码

很多细节问题需要考虑,这份代码只能算初步代码:

#coding=utf-8
import requests, re
import Image
from pytesseract import * def get_verification_code(url):
src = s.get(url).content
open('temp_pic',"wb").write(src)
pic=Image.open(r'./temp_pic')
return image_to_string(pic) login_url = 'http://www.pss-system.gov.cn/sipopublicsearch/wee/platform/wee_security_check'
host_url = 'http://www.pss-system.gov.cn/sipopublicsearch/portal/index.shtml'
pic_url = 'http://www.pss-system.gov.cn/sipopublicsearch/portal/login-showPic.shtml'
pic_mask = 'http://www.pss-system.gov.cn/sipopublicsearch/search/validateCode-showPic.shtml?params=2595D550022F3AC2E5D76ED4CAFD4D8E'
search_url = 'http://www.pss-system.gov.cn/sipopublicsearch/search/smartSearch-executeSmartSearch.shtml'
show_page = 'http://www.pss-system.gov.cn/sipopublicsearch/search/showSearchResult-startWa.shtml'
show_list = 'http://www.pss-system.gov.cn/sipopublicsearch/search/search/showViewList.shtml'
mask_check_url = 'http://www.pss-system.gov.cn/sipopublicsearch/search/validateMask.shtml'
download_url = 'http://www.pss-system.gov.cn/sipopublicsearch/search/downloadLitera.do' down_head = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'en-US,en;q=0.8',
'Cache-Control':'max-age=0',
'Connection':'keep-alive',
'Content-Type':'application/x-www-form-urlencoded',
'Origin':'http://www.pss-system.gov.cn',
'Referer':'http://www.pss-system.gov.cn/sipopublicsearch/search/search/showViewList.shtml',
'Upgrade-Insecure-Requests':'1',
'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36'
} s = requests.session()
cookies = dict(cookies_are='working')
s.get(host_url) vcode = get_verification_code(pic_url)
login_data = {
'j_validation_code':vcode,
'j_loginsuccess_url':'http://www.pss-system.gov.cn/sipopublicsearch/portal/index.shtml',
'j_username':'emhhbmdzYW4xMjM=',
'j_password':'emhhbmdzYW4xMjM='
} s.post(login_url,data=login_data,cookies=cookies)
print "login!!\n-------------------" keywords = raw_input("please input keywords: ")
cnt = 0 search_data = {
'searchCondition.searchExp':keywords,
'searchCondition.dbId':'VDB',
'searchCondition.searchType':'Sino_foreign',
'wee.bizlog.modulelevel':'0200101'
} result = s.post(search_url,data=search_data,cookies=cookies).content
total = int(re.search(r' 共.*?页 (.*?)条数据',result,re.S).group(1))
print "total:",total
all_result = re.findall('javascript:viewLitera_search\(\'.*?\',\'(.*?)\',\'single\'\)',result,re.S) executableSearchExp = "VDB:(TBI=" + "'" + keywords + "')"
literatureSF = "复合文本=(" + keywords + ")" while cnt <= total:
cnt += 10
for cur in all_result:
real_id = cur
if 'CN' in cur :
real_id = cur[:14] + '.' + cur[14:] condition = r"VDB:(ID='" + real_id + r"')"
cnName = '检索式:复合文本=' + '(' + keywords + ')'
srcEnName = 'SearchStatement:复合文本=' + '(' + keywords + ')' print real_id data_cur = {
'viewQC.viewLiteraQCList[0].srcCnName':cnName,
'viewQC.viewLiteraQCList[0].srcEnName':srcEnName,
'viewQC.viewLiteraQCList[0].searchStrategy':'',
'viewQC.viewLiteraQCList[0].searchCondition.executableSearchExp':condition,
'viewQC.viewLiteraQCList[0].searchCondition.sortFields':'-APD,+PD',
'viewQC.needSearch':'true',
'viewQC.type':'SEARCH',
'wee.bizlog.modulelevel':'0200604'
} show = s.post(show_list,data = data_cur,cookies=cookies).content tmp = re.search('literaList\[0\] = \{(.*?)\};',show,re.S)
if tmp == None:
break; idlist = re.findall('"(.*?)"',tmp.group(1).replace(' ',''),re.S) # 解析验证码
vcode = get_verification_code(pic_mask)
print vcode # 获取加密后的mask
mask_data = {
'':'',
'wee.bizlog.modulelevel':'02016',
'mask':vcode
}
kao = s.post(mask_check_url,data=mask_data,cookies=cookies).content
#{"downloadCount":2,"downloadItems":null,"mask":"1a75026a-5138-4460-a35e-5ef60258d1d0","pass":true,"sid":null}
mask_jm = re.search(r'"mask":"(.*?)"',kao,re.S).group(1)
#print mask_jm data_down = {
'wee.bizlog.modulelevel':'02016',
'checkItems':'abstractCheck',
'__checkbox_checkItems':'abstractCheck',
'checkItems':'TIVIEW',
'checkItems':'APO',
'checkItems':'APD',
'checkItems':'PN',
'checkItems':'PD',
'checkItems':'ICST',
'checkItems':'PAVIEW',
'checkItems':'INVIEW',
'checkItems':'PR',
'checkItems':'ABVIEW',
'checkItems':'ABSIMG',
'idList[0].id':idlist[0],
'idList[0].pn':idlist[3],
'idList[0].an':idlist[2],
'idList[0].lang':idlist[4],
'checkItems':'fullTextCheck',
'__checkbox_checkItems':'fullTextCheck',
'checkItems':'fullImageCheck',
'__checkbox_checkItems':'fullImageCheck',
'mask':mask_jm
} down_page = s.post(download_url,data=data_down,headers=down_head,cookies=cookies).content open( cur + ".zip" ,"wb").write(down_page) kao_data = {
"resultPagination.limit":"10",
"resultPagination.sumLimit":"10",
"resultPagination.start":cnt,
"resultPagination.totalCount":total,
"searchCondition.searchType":"Sino_foreign",
"searchCondition.dbId":"",
"searchCondition.extendInfo['MODE']":"MODE_GENERAL",
"searchCondition.searchExp":keywords,
"wee.bizlog.modulelevel":"0200101",
"searchCondition.executableSearchExp":executableSearchExp,
"searchCondition.literatureSF":literatureSF,
"searchCondition.strategy":"",
"searchCondition.searchKeywords":"",
"searchCondition.searchKeywords":keywords
}
result = s.post(show_page,data=kao_data,cookies=cookies).content
print "next page"
all_result = re.findall('javascript:viewLitera_search\(\'.*?\',\'(.*?)\',\'single\'\)',result,re.S)

7. 效果展示

8. TODO

可能会继续更新 code

python爬虫学习(10) —— 专利检索DEMO的更多相关文章

  1. python爬虫学习 —— 总目录

    开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...

  2. python爬虫学习(1) —— 从urllib说起

    0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...

  3. Python爬虫学习第一记 (翻译小助手)

    1 # Python爬虫学习第一记 8.24 (代码有点小,请放大看吧) 2 3 #实现有道翻译,模块一: $fanyi.py 4 5 import urllib.request 6 import u ...

  4. Python爬虫学习:三、爬虫的基本操作流程

    本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...

  5. Python爬虫学习:四、headers和data的获取

    之前在学习爬虫时,偶尔会遇到一些问题是有些网站需要登录后才能爬取内容,有的网站会识别是否是由浏览器发出的请求. 一.headers的获取 就以博客园的首页为例:http://www.cnblogs.c ...

  6. Python爬虫学习:二、爬虫的初步尝试

    我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...

  7. 《Python爬虫学习系列教程》学习笔记

    http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...

  8. python爬虫学习视频资料免费送,用起来非常666

    当我们浏览网页的时候,经常会看到像下面这些好看的图片,你是否想把这些图片保存下载下来. 我们最常规的做法就是通过鼠标右键,选择另存为.但有些图片点击鼠标右键的时候并没有另存为选项,或者你可以通过截图工 ...

  9. python爬虫学习笔记(一)——环境配置(windows系统)

    在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库)   [推荐地址:清华镜像] https://mirrors ...

随机推荐

  1. 【Win 10 应用开发】TCP通信过程

    基于TCP协议的通信,估计大伙儿都不陌生的,以前玩.net或玩C++的时候应该玩得很多吧.现在老周简单介绍一下在RT中如何用. TCP是基于连接的,所以,肯定有一方是监听者,通常称服务端或服务器,它负 ...

  2. NET Core-学习笔记(一)

    .net core最近园子讨论频率很高的话题,从不久前发布正式版本后,也是开始从netcore官网一步一步走向学习之路:.net跨平台的设计让人很是兴奋起来,因为做了多年的互联网研发者,见识了很多一流 ...

  3. MySQL binlog中的事件类型

    MySQL binlog记录的所有操作实际上都有对应的事件类型的,譬如STATEMENT格式中的DML操作对应的是QUERY_EVENT类型,ROW格式下的DML操作对应的是ROWS_EVENT类型. ...

  4. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  5. Install and Configure SharePoint 2013 Workflow

    这篇文章主要briefly introduce the Install and configure SharePoint 2013 Workflow. Microsoft 推出了新的Workflow ...

  6. 跟我学习NHibernate (1)

    引言:Nibernate概述 NHibernate是一个ORM框架,NHibernate是一个把C#对象世界和关系世界数据库之间联系起来的一座桥梁.NHibernate 能自动映射实体模型到数据库,所 ...

  7. 用CIL写程序:从“call vs callvirt”看方法调用

    前文回顾:<用CIL写程序系列> 前言: 最近的时间都奉献给了加班,距离上一篇文章也有半个多月了.不过在上一篇文章<用CIL写程序:定义一个叫“慕容小匹夫”的类>中,匹夫和各位 ...

  8. C#测试题

    阅读下面的程序,程序运行后hovertree值为( ) int x = 3, y = 4, z = 5;String s = "xyz";string hovertree = s ...

  9. 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(六)

    (六)在Website Cloud中添加site 1新建Website,并打开 使用前面创建的用户 newbee@waplab.com 登录租户Portal,新建一个website 新建完成后, 可以 ...

  10. MVC发布到虚拟主机上出现的错误

    问题1:无法识别的属性“targetFramework”.请注意属性名称区分大小写. 现象:无法识别的属性“targetFramework”.请注意属性名称区分大小写. 原因:站点中部署的Web使用的 ...