怎么在Python中使用Scrapy爬虫框架

  介绍

怎么在Python中使用Scrapy爬虫框架?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

<强>一、创建Scrapy项目

<代码> Scrapy startproject腾讯

命令执行后,会创建一个腾讯文件夹,结构如下

<强>二、编写项文件,根据需要爬取的内容定义爬取字段

#, - *安康;编码:utf-8  - * -   import  scrapy   class  TencentItem (scrapy.Item):   #才能,职位名   时间=positionname 才能;scrapy.Field ()   #才能,详情连接   时间=positionlink 才能;scrapy.Field ()   #才能,职位类别   时间=positionType 才能;scrapy.Field ()   #,才能招聘人数   时间=peopleNum 才能;scrapy.Field ()   #才能,工作地点   时间=workLocation 才能;scrapy.Field ()   #,才能发布时间   publishTime 才能=,scrapy.Field ()

<强>三、编写蜘蛛文件

进入腾讯目录,使用命令创建一个基础爬虫类:

#, tencentPostion为爬虫名,tencent.com为爬虫作用范围   scrapy  genspider  tencentPostion “tencent.com"

执行命令后会在蜘蛛文件夹中创建一个tencentPostion.py的文件,现在开始对其编写:

#, - *安康;编码:utf-8  - * -   import  scrapy   得到tencent.items  import  TencentItem   class  TencentpositionSpider (scrapy.Spider):   “才能”;“   ,,功能:爬取腾讯社招信息   “才能”;“   #,才能爬虫名   name =,才能“tencentPosition"   #,才能爬虫作用范围   allowed_domains 才能=,(“tencent.com")   url =,才能“http://hr.tencent.com/position.php?&开始=?   offset 才能=0   #,才能起始url   start_urls 才能=,[url  +, str(抵消)]   def 才能解析(自我,,反应):   ,,,for  each 拷贝response.xpath (“//tr (@class=& # 39;甚至# 39;],|,//tr (@class=& # 39;奇怪的# 39;]“):   ,,,,,#,初始化模型对象   ,,,,,item =, TencentItem ()   ,,,,,#,职位名称   ,,,,,项目[& # 39;positionname& # 39;],=, each.xpath (“。/td [1]//text ()“) .extract () [0]   ,,,,,#,详情连接   ,,,,,项目[& # 39;positionlink& # 39;],=, each.xpath (“。/td [1]//@href") .extract () [0]   ,,,,,#,职位类别   ,,,,,项目[& # 39;positionType& # 39;],=, each.xpath (“。/td [2]/text ()“) .extract () [0]   ,,,,,,号招聘人数   ,,,,,项目[& # 39;peopleNum& # 39;],=, each.xpath (“。/td [3]/text ()“) .extract () [0]   ,,,,,#,工作地点   ,,,,,项目[& # 39;workLocation& # 39;],=, each.xpath (“。/td [4]/text ()“) .extract () [0]   ,,,,,#,发布时间   ,,,,,项目[& # 39;publishTime& # 39;],=, each.xpath (“。/td [5]/text ()“) .extract () [0]   ,,,,,油品收率   ,,,if  self.offset  & lt;, 1680:   ,,,,,self.offset  +=10   ,,,#,每次处理完一页的数据之后,重新发送下一页页面请求   ,,,#,self.offset自增10,同时拼接为新的url,并调用回调函数self.parse处理反应   ,,,油品收率scrapy.Request (self.url  +, str (self.offset), callback =, self.parse)

<强>四、编写管道文件

#, - *安康;编码:utf-8  - * -   import  json   class  TencentPipeline(对象):   “““,   ,,,功能:保存条目数据,   “才能”;“   def 才能__init__(自我):   ,,,self.filename =,开放(“tencent.json",,“w")   def 才能;process_item(自我,,,,蜘蛛):   ,,,text =, json.dumps (dict(项),ensure_ascii =, False), +,,, \ n"   ,,,self.filename.write (text.encode (“utf-8"))   ,,,return 项目   def 才能close_spider(自我,,蜘蛛):   ,,,self.filename.close ()

<强>五、设置文件设置(主要设置内容)

#,设置请求头部,添加url   DEFAULT_REQUEST_HEADERS =, {   “才能User-Agent",:,“Mozilla/5.0,(兼容;,MSIE  9.0;, Windows  NT  6.1;,三叉戟/5.0;“,   & # 39;才能接受# 39;:,& # 39;text/html, application/xhtml + xml应用程序/xml; q=0.9 */*; q=0.8 & # 39;   }   #,设置项——管道   ITEM_PIPELINES =, {   & # 39;才能tencent.pipelines.TencentPipeline& # 39;:, 300年,   }

执行命令,运行程序

#, tencentPosition为爬虫名   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Python中使用Scrapy爬虫框架