python爬虫学习(10) —— 专利检索DEMO
这是一个稍微复杂的demo,它的功能如下:
- 输入专利号,下载对应的专利文档
- 输入关键词,下载所有相关的专利文档
0. 模块准备
- 首先是requests,这个就不说了,爬虫利器
- 其次是安装tesseract-ocr,pytesseract 和 PIL 「用于识别验证码」
1. 模拟登陆
我们需要对 这个网站 专利检索及分析 进行分析,反复鼓捣之后发现,找不到下载链接?
tell my why? 原来是没有登陆。 果然,登陆之后能够找到下载按钮,并手动下载成功。
注意到,在登陆和下载的同时,还需要输入验证码。
这样,我们第一步要解决的问题,便是验证码识别与模拟登陆
ubuntu下使用:
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的更多相关文章
- python爬虫学习 —— 总目录
开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...
- python爬虫学习(1) —— 从urllib说起
0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...
- Python爬虫学习第一记 (翻译小助手)
1 # Python爬虫学习第一记 8.24 (代码有点小,请放大看吧) 2 3 #实现有道翻译,模块一: $fanyi.py 4 5 import urllib.request 6 import u ...
- Python爬虫学习:三、爬虫的基本操作流程
本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:三.爬虫的基本操作与流程 一般我们使用Python爬虫都是希望实现一套完整的功能,如下: 1.爬虫目标数据.信息: 2.将 ...
- Python爬虫学习:四、headers和data的获取
之前在学习爬虫时,偶尔会遇到一些问题是有些网站需要登录后才能爬取内容,有的网站会识别是否是由浏览器发出的请求. 一.headers的获取 就以博客园的首页为例:http://www.cnblogs.c ...
- Python爬虫学习:二、爬虫的初步尝试
我使用的编辑器是IDLE,版本为Python2.7.11,Windows平台. 本文是博主原创随笔,转载时请注明出处Maple2cat|Python爬虫学习:二.爬虫的初步尝试 1.尝试抓取指定网页 ...
- 《Python爬虫学习系列教程》学习笔记
http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多.学习过程中我把一些学习的笔记总结下来,还记录了一些自己 ...
- python爬虫学习视频资料免费送,用起来非常666
当我们浏览网页的时候,经常会看到像下面这些好看的图片,你是否想把这些图片保存下载下来. 我们最常规的做法就是通过鼠标右键,选择另存为.但有些图片点击鼠标右键的时候并没有另存为选项,或者你可以通过截图工 ...
- python爬虫学习笔记(一)——环境配置(windows系统)
在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库) [推荐地址:清华镜像] https://mirrors ...
随机推荐
- Android ButterKnife配置使用
ButterKnife在GitHub的地址:https://github.com/JakeWharton/butterknife 最新的版本是:8.4.0 app 模块的build.gradle: a ...
- 前端学PHP之MemCache
× 目录 [1]作用 [2]安装 [3]管理[4]命令 前面的话 Memcache是一个高性能的分布式的内存对象缓存系统,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(15)-权限管理系统准备
系列目录 这节我们说下权限系统的特点,本系统采用的是MVC4+EF5+IOC 接口编程的架构,其中的权限树用的是DWTree,功能上做到灵活,授权操控细致,权限可以细到按钮级别 ,为了部署简单,导致设 ...
- 2016网易春招Java在线笔试回忆录
别看是在线笔试,但是非常严格,全称窗口不得最小化和关闭,转移,全称需要打开摄像头监控,使用草稿纸需要摄像头对准……反正2个小时,题量在那儿摆着,有作弊的功夫不如好好做做最后的编程题呢……网易不让泄漏原 ...
- 数据库进阶之路(五) - MySQL行锁深入研究
由于业务逻辑的需要,必须对数据表的一行或多行加入行锁,举个最简单的例子,图书借阅系统:假设id=1的这本书库存为1,但是有2个人同时来借这本书,此处的逻辑为: ; --如果restnum大于0,执行u ...
- 简述9种社交概念 SNS究竟用来干嘛?
1.QQ 必备型交流工具基本上每一个网民最少有一个QQ,QQ已经成为网民的标配,网络生活中已经离不开QQ了.虽然大家嘴上一直在骂 QQ这个不好,那个不对,但是很少有人能彻底离开QQ.QQ属于IM软件, ...
- Socket简单使用
客户端代码: import java.io.*; import java.net.*; public class DailyAdviceClient { public void go(){ try{ ...
- ASP模拟POST请求异步提交数据的方法
这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限 ...
- 升级Centos的python为2.7(centos python 升级)
1.首先下载python的2.7.8版本的tar包.为什么没有选择最新版本的3.4呢?因为3版本的兼容性不好.很多一些成熟的项目都是基于2.7开发的. https://www.python.org/d ...
- Android开发者的Kotlin:书
原文标题:Kotlin for Android Developers: The book 原文链接:http://antonioleiva.com/kotlin-android-developers/ ...