首页 文章 精选 留言 我的

精选列表

搜索[索引崩溃],共10000篇文章
优秀的个人博客,低调大师

23、 Python快速开发分布式搜索引擎Scrapy精讲—craw母版l创建自动爬虫文件—以及 scrapy item loader机制

转: http://www.bdyss.cn http://www.swpan.cn 用命令创建自动爬虫文件 创建爬虫文件是根据scrapy的母版来创建爬虫文件的 scrapy genspider -l查看scrapy创建爬虫文件可用的母版 Available templates:母版说明 basic 创建基础爬虫文件 crawl 创建自动爬虫文件 csvfeed 创建爬取csv数据爬虫文件 xmlfeed 创建爬取xml数据爬虫文件 创建一个基础母版爬虫,其他同理 scrapy genspider -t 母版名称 爬虫文件名称 要爬取的域名创建一个基础母版爬虫,其他同理如:scrapy genspider -tcrawllagou www.lagou.com 第一步,配置items.py接收数据字段 default_output_processor = TakeFirst()默认利用ItemLoader类,加载items容器类填充数据,是列表类型,可以通过TakeFirst()方法,获取到列表里的内容 input_processor = MapCompose(预处理函数)设置数据字段的预处理函数,可以是多个函数 #-*-coding:utf-8-*- #Defineherethemodelsforyourscrapeditems # #Seedocumentationin: #http://doc.scrapy.org/en/latest/topics/items.html #items.py,文件是专门用于,接收爬虫获取到的数据信息的,就相当于是容器文件 importscrapy fromscrapy.loader.processorsimportMapCompose,TakeFirst fromscrapy.loaderimportItemLoader#导入ItemLoader类也就加载items容器类填充数据 classLagouItemLoader(ItemLoader):#自定义Loader继承ItemLoader类,在爬虫页面调用这个类填充数据到Item类 default_output_processor=TakeFirst()#默认利用ItemLoader类,加载items容器类填充数据,是列表类型,可以通过TakeFirst()方法,获取到列表里的内容 deftianjia(value):#自定义数据预处理函数 return'叫卖录音网'+value#将处理后的数据返给Item classLagouItem(scrapy.Item):#设置爬虫获取到的信息容器类 title=scrapy.Field(#接收爬虫获取到的title信息 input_processor=MapCompose(tianjia),#将数据预处理函数名称传入MapCompose方法里处理,数据预处理函数的形式参数value会自动接收字段title ) 第二步,编写自动爬虫与利用ItemLoader类加载items容器类填充数据 自动爬虫Rule()设置爬虫规则 参数: LinkExtractor()设置url规则 callback='回调函数名称' follow=True 表示在抓取页面继续深入 LinkExtractor()对爬虫获取到的url做规则判断处理 参数: allow= r'jobs/' 是一个正则表达式,表示符合这个url格式的,才提取 deny= r'jobs/' 是一个正则表达式,表示符合这个url格式的,不提取抛弃掉,与allow相反 allow_domains= www.lagou.com/ 表示这个域名下的连接才提取 deny_domains= www.lagou.com/ 表示这个域名下的连接不提取抛弃 restrict_xpaths= xpath表达式 表示可以用xpath表达式限定爬虫只提取一个页面指定区域的URL restrict_css= css选择器,表示可以用css选择器限定爬虫只提取一个页面指定区域的URL tags= 'a' 表示爬虫通过a标签去寻找url,默认已经设置,默认即可 attrs= 'href' 表示获取到a标签的href属性,默认已经设置,默认即可 * 利用自定义Loader类继承ItemLoader类,加载items容器类填充数据 *ItemLoader()实例化一个ItemLoader对象来加载items容器类,填充数据,如果是自定义Loader继承的ItemLoader同样的用法 参数: 第一个参数:要填充数据的items容器类注意加上括号, 第二个参数:response* ItemLoader对象下的方法: add_xpath('字段名称','xpath表达式')方法,用xpath表达式获取数据填充到指定字段 add_css('字段名称','css选择器')方法,用css选择器获取数据填充到指定字段 add_value('字段名称',字符串内容)方法,将指定字符串数据填充到指定字段 load_item()方法无参,将所有数据生成,load_item()方法被yield后数据被填充items容器指定类的各个字段 爬虫文件 #-*-coding:utf-8-*- importscrapy fromscrapy.linkextractorsimportLinkExtractor fromscrapy.spidersimportCrawlSpider,Rule fromadc.itemsimportLagouItem,LagouItemLoader#导入items容器类,和ItemLoader类 classLagouSpider(CrawlSpider):#创建爬虫类 name='lagou'#爬虫名称 allowed_domains=['www.luyin.org']#起始域名 start_urls=['http://www.luyin.org/']#起始url rules=( #配置抓取列表页规则 Rule(LinkExtractor(allow=('ggwa/.*')),follow=True), #配置抓取内容页规则 Rule(LinkExtractor(allow=('post/\d+.html.*')),callback='parse_job',follow=True), ) defparse_job(self,response):#回调函数,注意:因为CrawlS模板的源码创建了parse回调函数,所以切记我们不能创建parse名称的函数 #利用ItemLoader类,加载items容器类填充数据 item_loader=LagouItemLoader(LagouItem(),response=response) item_loader.add_xpath('title','/html/head/title/text()') article_item=item_loader.load_item() yieldarticle_item items.py文件与爬虫文件的原理图 【转载自:http://www.lqkweb.com】

优秀的个人博客,低调大师

ES里设置索引中倒排列表仅仅存文档ID——采用docs存储后可以降低pos文件和cfs文件大小

index_options Theindex_optionsparameter controls what information is added to the inverted index, for search and highlighting purposes. It accepts the following settings: docs Only the doc number is indexed. Can answer the questionDoes this term exist in this field? freqs Doc number and term frequencies are indexed. Term frequencies are used to score repeated terms higher than single terms. positions Doc number, term frequencies, and term positions (or order) are indexed. Positions can be used forproximity or phrase queries. offsets Doc number, term frequencies, positions, and start and end character offsets (which map the term back to the original string) are indexed. Offsets are used by thepostings highlighter. Analyzedstring fields usepositionsas the default, and all other fields usedocsas the default. PUT my_index { "mappings": { "my_type": { "properties": { "text": { "type": "text", "index_options": "offsets" } } } } } PUT my_index/my_type/1 { "text": "Quick brown fox" } GET my_index/_search { "query": { "match": { "text": "brown fox" } }, "highlight": { "fields": { "text": {} } } } COPY AS CURL VIEW IN CONSOLE Thetextfield will use the postings highlighter by default becauseoffsetsare indexed. 转自:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-options.html 注意:ES2.41里没有text这个type curl -XPUT 'http://localhost:9200/hec_test2' -d ' { "mappings": { "hec_type2": { "properties": { "filed-0": { "type": "string", "index_options": "docs" }, "filed-1": { "type": "string", "index_options": "docs" } } } } } ' 比较测试: 可以比默认的节省10+%的存储空间! 本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6397522.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

ES索引瘦身 压缩——_source _all 均disable filed store为no,引入第三方DB存储原始数据,去掉pos倒排和...

原始数据:835MB ES 设置了_source _all disabled 且设置了仅仅存docs倒排 Wed Feb 22 11:58:27 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 435 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 11:59:43 CST 2017 done 不做任何压缩: Wed Feb 22 14:56:04 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 1656 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 14:57:54 CST 2017 done curl -XPOST 'localhost:9200/hec_test3/_forcemerge' bone@PEK1000074003:~/test_data/splunk_es_cmp$ du -sm ~/elasticsearch-2.4.1/data/ 969 /home/bone/elasticsearch-2.4.1/data/ _all disabled: Wed Feb 22 15:22:09 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 1576 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 15:23:25 CST 2017 done merge后: 787 _source disabled: field store为false 搜索不到东西了 如果不存DB的话 Wed Feb 22 15:26:06 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 1288 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 15:27:45 CST 2017 done merge后: 512 _source disabled: field store为yes Wed Feb 22 15:33:28 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 1583 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 15:34:45 CST 2017 done merge后: 780 最终设置: curl -XPUT 'http://localhost:9200/hec_test3' -d ' { "mappings": { "hec_type3": { "_source": { "enabled": false }, "_all": { "enabled": false }, "properties": { "event": { "type": "string", "include_in_all": false, "doc_values": false, "index_options": "docs" } } } } } ' 压缩配置: index.codec: best_compression bash test_es24.sh Wed Feb 22 15:39:39 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 892 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 15:40:36 CST 2017 done merge后: 202 MB 50个字段拆分后: 用上述最终设置的效果: Wed Feb 22 16:02:59 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 1423 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 16:05:04 CST 2017 done merge后:686 不做任何配置: Wed Feb 22 16:08:30 CST 2017 Before size: 1 /home/bone/elasticsearch-2.4.1/data/ OK, waiting... 0 After size: 2641 /home/bone/elasticsearch-2.4.1/data/ Wed Feb 22 16:11:54 CST 2017 done merge后: 1650 本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6430378.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

22、Python快速开发分布式搜索引擎Scrapy精讲—scrapy模拟登陆和知乎倒立文字验证码识别

转自: http://www.bdyss.cn http://www.swpan.cn 第一步。首先下载,大神者也的倒立文字验证码识别程序 下载地址:https://github.com/muchrooms/zheye 注意:此程序依赖以下模块包 Keras==2.0.1 Pillow==3.4.2 jupyter==1.0.0 matplotlib==1.5.3 numpy==1.12.1 scikit-learn==0.18.1 tensorflow==1.0.1 h5py==2.6.0 numpy-1.13.1+mkl 我们用豆瓣园来加速安以上依赖装如: pipinstall-ihttps://pypi.douban.com/simpleh5py==2.6.0 如果是win系统,可能存在安装失败的可能,如果那个包安装失败,就到 http://www.lfd.uci.edu/~gohlke/pythonlibs/ 找到win对应的版本下载到本地安装,如: pipinstallh5py-2.7.0-cp35-cp35m-win_amd64.whl 第二步,将者也的,验证码识别程序的zheye文件夹放到工程目录里 第三步,爬虫实现 start_requests()方法,起始url函数,会替换start_urls Request()方法,get方式请求网页 url=字符串类型url headers=字典类型浏览器代理 meta=字典类型的数据,会传递给回调函数 callback=回调函数名称 scrapy.FormRequest()post方式提交数据 url=字符串类型url headers=字典类型浏览器代理 meta=字典类型的数据,会传递给回调函数 callback=回调函数名称 formdata=字典类型,要提交的数据字段 response.headers.getlist('Set-Cookie')获取响应Cookiesresponse.request.headers.getlist('Cookie')获取请求Cookies #-*-coding:utf-8-*- importscrapy fromscrapy.httpimportRequest,FormRequest importre classPachSpider(scrapy.Spider):#定义爬虫类,必须继承scrapy.Spider name='pach'#设置爬虫名称 allowed_domains=['zhihu.com']#爬取域名 #start_urls=['']#爬取网址,只适于不需要登录的请求,因为没法设置cookie等信息 header={'User-Agent':'Mozilla/5.0(WindowsNT10.0;WOW64;rv:54.0)Gecko/20100101Firefox/54.0'}#设置浏览器用户代理 defstart_requests(self):#起始url函数,会替换start_urls """第一次请求一下登录页面,设置开启cookie使其得到cookie,设置回调函数""" return[Request( url='https://www.zhihu.com/#signin', headers=self.header, meta={'cookiejar':1},#开启Cookies记录,将Cookies传给回调函数 callback=self.parse )] defparse(self,response): #响应Cookies Cookie1=response.headers.getlist('Set-Cookie')#查看一下响应Cookie,也就是第一次访问注册页面时后台写入浏览器的Cookie print('后台首次写入的响应Cookies:',Cookie1) #获取xsrf密串 xsrf=response.xpath('//input[@name="_xsrf"]/@value').extract()[0] print('获取xsrf密串:'+xsrf) #获取验证码 importtime t=str(int(time.time()*1000)) captcha_url='https://www.zhihu.com/captcha.gif?r={0}&type=login&lang=cn'.format(t)#构造验证码请求地址 yieldRequest(url=captcha_url,#请求验证码图片 headers=self.header, meta={'cookiejar':response.meta['cookiejar'],'xsrf':xsrf},#将Cookies和xsrf密串传给回调函数 callback=self.post_tj ) defpost_tj(self,response): withopen('yzhm.jpg','wb')asf:#打开图片句柄 f.write(response.body)#将验证码图片写入本地 f.close()#关闭句柄 #---------------------------者也验证码识别----------------------- fromzheyeimportzheye#导入者也倒立文字验证码识别模块对象 z=zheye()#实例化对象 positions=z.Recognize('yzhm.jpg')#将验证码本地路径传入Recognize方法识别,返回倒立图片的坐标 #print(positions)#默认倒立文字的y坐标在前,x坐标在后 #知乎网要求的倒立文字坐标是x轴在前,y轴在后,所以我们需要定义一个列表来改变默认的,倒立文字坐标位置 pos_arr=[] iflen(positions)==2: ifpositions[0][1]>positions[1][1]:#判断列表里第一个元祖里的第二个元素如果大于,第二个元祖里的第二个元素 pos_arr.append([positions[1][1],positions[1][0]]) pos_arr.append([positions[0][1],positions[0][0]]) else: pos_arr.append([positions[0][1],positions[0][0]]) pos_arr.append([positions[1][1],positions[1][0]]) else: pos_arr.append([positions[0][1],positions[0][0]]) print('处理后的验证码坐标',pos_arr) #-------------者也验证码识别结束-------- iflen(pos_arr)==2: data={#设置用户登录信息,对应抓包得到字段 '_xsrf':response.meta['xsrf'], 'password':'279819', 'captcha':'{"img_size":[200,44],"input_points":[[%.2f,%f],[%.2f,%f]]}'%( pos_arr[0][0]/2,pos_arr[0][1]/2,pos_arr[1][0]/2,pos_arr[1][1]/2),#因为验证码识别默认是400X88的尺寸所以要除以2 'captcha_type':'cn', 'phone_num':'15284816568' } else: data={#设置用户登录信息,对应抓包得到字段 '_xsrf':response.meta['xsrf'], 'password':'279819', 'captcha':'{"img_size":[200,44],"input_points":[[%.2f,%f]]}'%( pos_arr[0][0]/2,pos_arr[0][1]/2), 'captcha_type':'cn', 'phone_num':'15284816568' } print('登录提交数据',data) print('登录中....!') """第二次用表单post请求,携带Cookie、浏览器代理、用户登录信息,进行登录给Cookie授权""" return[scrapy.FormRequest( url='https://www.zhihu.com/login/phone_num',#真实post地址 meta={'cookiejar':response.meta['cookiejar']},#接收第传过来的Cookies headers=self.header, formdata=data, callback=self.next )] defnext(self,response): #请求Cookie Cookie2=response.request.headers.getlist('Cookie') print('登录时携带请求的Cookies:',Cookie2) jieg=response.body.decode("utf-8")#登录后可以查看一下登录响应信息 print('登录响应结果:',jieg) print('正在请需要登录才可以访问的页面....!') """登录后请求需要登录才能查看的页面,如个人中心,携带授权后的Cookie请求""" yieldRequest( url='https://www.zhihu.com/people/lin-gui-xiu-41/activities', headers=self.header, meta={'cookiejar':True}, callback=self.next2 ) defnext2(self,response): #请求Cookie Cookie3=response.request.headers.getlist('Cookie') print('查看需要登录才可以访问的页面携带Cookies:',Cookie3) leir=response.xpath('/html/head/title/text()').extract()#得到个人中心页面 print('最终内容',leir) #print(response.body.decode("utf-8")) 【转载自:http://www.lqkweb.com】

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册