Scrapy——2 Scrapy shell
什么是Scrapy shell
Scrapy shell终端是一个交互终端,我们可以在未启动spider的情况下尝试调试代码,也可以用来测试Xpath或CSS表达式,查看他们的工作方式,方便我们爬取的网页中提取数据
Scrapy内置选择器:
什么是sipder
Spider类定义了如何爬取某个(或某些)网站。包括了爬取的动作(例如:是否跟进链接)以及如何从网页的内容中提取结构化数据(爬取item)。 换句话说,Spider就是您定义爬取的动作及分析某个网页(或者是有些网页)的地方
实战
当我们爬取腾讯的招聘信息时,假设我们需要的数据是,岗位名称,岗位方向,招聘人数,工作地点,发布时间,需求,并将他们分别Mongo和Mysql数据库保存
我们需要匹配多个数据,可以先用scrapy shell url 先尝试解析,命令回车,会进入python的交互模式,如果安装了ipython,
会优先进入ipython的环境。
然后responde已经默认请求完成。直接response.xpath('//**')就可以解析数据,非常方便
在项目开发中非常实用
import scrapyclass TencentItem(scrapy.Item):# define the fields for your item here like:# name = scrapy.Field()# 名字work_name = scrapy.Field()# 类别work_category = scrapy.Field()# 人数work_number = scrapy.Field()# 地点work_address = scrapy.Field()# 时间publish_time = scrapy.Field()# 内容work_content = scrapy.Field()
# Configure item pipelines # See .html ITEM_PIPELINES = {'tencent.pipelines.TencentPipeline': 300, }
# -*- coding: utf-8 -*- import scrapy from ..items import TencentItemclass TenSpiderSpider(scrapy.Spider):name = 'ten_spider'allowed_domains = ['tencent']start_urls = ['.php']base_url = '/'def parse(self, response):tr_list = response.xpath('//table[@class="tablelist"]//tr')[1:-1]next_url = response.xpath('//a[@id="next"]/@href').extract()[0]for tr in tr_list:items = TencentItem()detail_url = tr.xpath('./td[1]/a/@href').extract_first()items['work_name'] = tr.xpath('./td[1]/a/text()').extract_first()items['work_category'] = tr.xpath('./td[2]/text()').extract_first()items['work_number'] = tr.xpath('./td[3]/text()').extract_first()items['work_address'] = tr.xpath('./td[4]/text()').extract_first()items['publish_time'] = tr.xpath('./td[5]/text()').extract_first()yield scrapy.Request(url=self.base_url+detail_url,callback=self.detail_parse,meta={'items':items})yield scrapy.Request(url=self.base_url + next_url, callback=self.parse)def detail_parse(self,response):items = ('items')work_content = '|'.join(response.xpath('//ul[@class="squareli"]/li/text()').extract())items['work_content'] = work_contentyield items
# -*- coding: utf-8 -*-# Define your item pipelines here # # Don't forget to add your pipeline to the ITEM_PIPELINES setting # See: .htmlimport pymongo import pymysqlclass TencentPipeline(object):def __init__(self): = t(host='127.0.0.1',port=3306,user='pywjh',password='pywjh',db='bole',charset='utf8')self.cursor = ursor()def open_spider(self,spider):pass# = pymongo.MongoClient(host='127.0.0.1',port=27017)# self.db = ['tencent_db']# tion = self.db['job']def process_item(self, item, spider):# tion.insert(dict(item))sql = "insert into job(name, category, number, address, time, content) values(%s, %s, %s, %s, %s, %s)"ute(sql,[item['work_name'],item['work_category'],item['work_number'],item['work_address'],item['publish_time'],item['work_content']])it()return itemdef close_spider(self,spider):pass
结果:
Mongo
> show dbs admin (empty) local 0.078GB pywjh 0.078GB tencent_db 0.078GB test 0.078GB > use tencent_db switched to db tencent_db > show collections job system.indexes > db.job.find().pretty() {"_id" : ObjectId("5be12ee28a6e9e0b3cad0d25"),"work_category" : "技术类","publish_time" : "2018-11-06","work_name" : "22989-高级网络运维工程师","work_number" : "2","work_address" : "深圳","work_content" : "1.负责腾讯云机房网络、VPC、负载均衡平台的规划,建设,不断提升运维效率;|2.负责对网络问题分可用性;|4.负责分析业务不合理、不高效地方,提出优化改进方案并推进实施。|1.本科及以上学历;|2.3年以上相关工作经验,熟精通路由协议(ospf,bgp),有大规模网络规划、运维经验优先;|4.熟悉主流虚拟化技术原理(如kvm,xen,hyper-v,lxc),有实际的ows操作系统,对系统性能相关问题有较深刻理解;|6.擅长shell、python、perl中一种或几种,熟练应用awk、sed、grep、strace、tcpdump、gdb等常用命令。" } ................ ................ ................ {"_id" : ObjectId("5be12ee38a6e9e0b3cad0d38"),"work_category" : "市场类","publish_time" : "2018-11-06","work_name" : "25667-企点行业渠道销售经理(北京)","work_number" : "1","work_address" : "北京","work_content" : "负责企点产品的行业渠道拓展工作,包括ISV/SI的维护及跟进,达成制定的销售业绩指标;(主要是汽合作,快速实现拳头优势和标杆效应;|定期拜访渠道合作伙伴,充分了解客户需求并积极跟进,制定合理方案,负责方案提示、谈判务的市场化,帮助合作伙伴更加健康地发展。|本科及以上学历,5年以上渠道或业务管理经验,有saas、互联网广告行业工作经验优造性思维和营销推广能力。" }
Mysql
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | bole | | mysql | | performance_schema | | pywjh | | sys | +--------------------+ 6 rows in set (0.08 sec)mysql> use bole; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -ADatabase changed mysql> show tables; +----------------+ | Tables_in_bole | +----------------+ | blzx | | job | +----------------+ 2 rows in set (0.00 sec)mysql> select * from job; +----+------------------------------------------------------+------------------+--------+---------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | id | name | category | number | address | time | content | +----+------------------------------------------------------+------------------+--------+---------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 1 | 22989-高级网络运维工程师 | 技术类 | 2 | 深圳 | 2018-11-06 | 1.负载均衡平台的规划,建设,不断提升运维效率;|2.负责对网络问题分析解决,形成方法论,提升团队技术能力;|3.负责通过技术手以上学历;|2.3年以上相关工作经验,熟悉TCP/IP协议,了解SDN相关技术,能够定位linux虚拟化环境下网络异常;|3.熟悉主流的网技术原理(如kvm,xen,hyper-v,lxc),有实际的对虚拟化疑难问题trouble shooting经验;|5.精通linux,windows操作系统,对系统erl中一种或几种,熟练应用awk、sed、grep、strace、tcpdump、gdb等常用命令。 | | 2 | 22989-运营产品中心web前端开发 | 技术类 | 2 | 深圳 | 2018-11-06 | 负责工作;| 负责页面相关的接入层的开发(nodejs)。| 负责前端框架的搭建,公共组件的开发和维护。|本科以上学历,计算机相关专avascript,熟悉使用jQuery,react.js等框架及类库;| 熟悉常用WEB开发调试工具;| 有使用grunt、gulp、webpack等工具进行前作态度端正,能够积极主动去工作,高效推动项目完成;| 具有良好的逻辑思维及语言表达能沟通力,要能高效配合团队成员,共同 | ....................... ....................... .......................| 67 | SA-腾讯社交广告运维工程师(深圳) | 技术类 | 1 | 深圳 | 2018-11-06 | 负责存存储组件的运维,包括日常扩容,缩容,故障处理,演习,部署容灾跟进;|负责db,mysql的运维,包括db优化,故障处理,演习护产品的质量稳定,通过技术手段、流程制度提升组件的健壮性,可用性;|其他和以上工作相关的专项事务。|3年以上工作经验,精能相关问题有较深刻理解;|精通shell编程,熟练应用awk、sed、grep、strace、tcudump、gdb等常用命令;|精通mysql,对mysql相is,memcache,mongodb,leveldb等运维经验者优先。|熟练使用Linux/unix操作系统,熟悉主流虚拟化技术与开源组件,有devops实体系结构方面的知识;|熟悉集群高可用性方案,有一定带宽成本速度优化经验;|熟悉互联网产品的运维流程,有海量运营产品架构本科或以上学历,工作细致、善于思考,有很强的数据分析和问题解决能力。 | +----+------------------------------------------------------+------------------+--------+---------+------------+-------------------------------------------------------------------------------------------------------------------------------+ 67 rows in set (0.00 sec)
这里推荐一下pycharm里也有数据库,
转载于:.html
本文发布于:2024-01-31 23:22:19,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170671454032137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |