騰訊社招爬取
阿新 • • 發佈:2018-11-16
目標任務:爬取騰訊社招資訊,需要爬取的內容為:職位名稱,職位的詳情連結,職位類別,招聘人數,工作地點,釋出時間。
一、建立Scrapy專案
scrapy startproject Tencent
命令執行後,會建立一個Tencent資料夾,結構如下
二、編寫item檔案,根據需要爬取的內容定義爬取欄位
# -*- coding: 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()
三、編寫spider檔案
進入Tencent目錄,使用命令建立一個基礎爬蟲類:
# tencentPostion為爬蟲名,tencent.com為爬蟲作用範圍scrapy genspider tencentPostion "tencent.com"
執行命令後會在spiders資料夾中建立一個tencentPostion.py的檔案,現在開始對其編寫:
# -*- coding: utf-8 -*- import scrapy from tencent.items import TencentItem class TencentpositionSpider(scrapy.Spider): """ 功能:爬取騰訊社招資訊 """ # 爬蟲名
name = "tencentPosition"
# 爬蟲作用範圍 allowed_domains = ["tencent.com"] url = "http://hr.tencent.com/position.php?&start=" offset = 0 # 起始url start_urls = [url + str(offset)] def parse(self, response): for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"): # 初始化模型物件 item = TencentItem() # 職位名稱 item['positionname'] = each.xpath("./td[1]/a/text()").extract()[0] # 詳情連線 item['positionlink'] = each.xpath("./td[1]/a/@href").extract()[0] # 職位類別 item['positionType'] = each.xpath("./td[2]/text()").extract()[0] # 招聘人數 item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0] # 工作地點 item['workLocation'] = each.xpath("./td[4]/text()").extract()[0] # 釋出時間 item['publishTime'] = each.xpath("./td[5]/text()").extract()[0] yield item if self.offset < 1680: self.offset += 10 # 每次處理完一頁的資料之後,重新發送下一頁頁面請求 # self.offset自增10,同時拼接為新的url,並呼叫回撥函式self.parse處理Response yield scrapy.Request(self.url + str(self.offset), callback = self.parse)
四、編寫pipelines檔案
# -*- coding: utf-8 -*- import json class TencentPipeline(object):
"""
功能:儲存item資料
""" def __init__(self): self.filename = open("tencent.json", "w") def process_item(self, item, spider): text = json.dumps(dict(item), ensure_ascii = False) + ",\n" self.filename.write(text.encode("utf-8")) return item def close_spider(self, spider): self.filename.close()
五、settings檔案設定(主要設定內容)
# 設定請求頭部,新增url DEFAULT_REQUEST_HEADERS = { "User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;", 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' } # 設定item——pipelines ITEM_PIPELINES = { 'tencent.pipelines.TencentPipeline': 300, }
執行命令,執行程式
# tencentPosition為爬蟲名 scrapy crwal tencentPosition
使用CrawlSpider類改寫
# 建立專案 scrapy startproject TencentSpider # 進入專案目錄下,建立爬蟲檔案 scrapy genspider -t crawl tencent tencent.com
item等檔案寫法不變,主要是爬蟲檔案的編寫
# -*- coding:utf-8 -*- import scrapy # 匯入CrawlSpider類和Rule from scrapy.spiders import CrawlSpider, Rule # 匯入連結規則匹配類,用來提取符合規則的連線 from scrapy.linkextractors import LinkExtractor from TencentSpider.items import TencentItem class TencentSpider(CrawlSpider): name = "tencent" allow_domains = ["hr.tencent.com"] start_urls = ["http://hr.tencent.com/position.php?&start=0#a"] # Response裡連結的提取規則,返回的符合匹配規則的連結匹配物件的列表 pagelink = LinkExtractor(allow=("start=\d+")) rules = [ # 獲取這個列表裡的連結,依次傳送請求,並且繼續跟進,呼叫指定回撥函式處理 Rule(pagelink, callback = "parseTencent", follow = True) ] # 指定的回撥函式 def parseTencent(self, response): for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"): item = TencentItem() # 職位名稱 item['positionname'] = each.xpath("./td[1]/a/text()").extract()[0] # 詳情連線 item['positionlink'] = each.xpath("./td[1]/a/@href").extract()[0] # 職位類別 item['positionType'] = each.xpath("./td[2]/text()").extract()[0] # 招聘人數 item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0] # 工作地點 item['workLocation'] = each.xpath("./td[4]/text()").extract()[0] # 釋出時間 item['publishTime'] = each.xpath("./td[5]/text()").extract()[0] yield item
奔跑 、拼搏、何時才能擁有你的懷抱
目標任務:爬取騰訊社招資訊,需要爬取的內容為:職位名稱,職位的詳情連結,職位類別,招聘人數,工作地點,釋出時間。
一、建立Scrapy專案
scrapy startproject Tencent
命令執行後,會建立一個Tencent資料夾,結構如下
二、編寫item檔案,根據需要爬取的內容定義爬取欄位
# -*- coding: 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()
三、編寫spider檔案
進入Tencent目錄,使用命令建立一個基礎爬蟲類:
# tencentPostion為爬蟲名,tencent.com為爬蟲作用範圍 scrapy genspider tencentPostion "tencent.com"
執行命令後會在spiders資料夾中建立一個tencentPostion.py的檔案,現在開始對其編寫:
# -*- coding: utf-8 -*- import scrapy from tencent.items import TencentItem class TencentpositionSpider(scrapy.Spider): """ 功能:爬取騰訊社招資訊 """ # 爬蟲名
name = "tencentPosition"
# 爬蟲作用範圍 allowed_domains = ["tencent.com"] url = "http://hr.tencent.com/position.php?&start=" offset = 0 # 起始url start_urls = [url + str(offset)] def parse(self, response): for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"): # 初始化模型物件 item = TencentItem() # 職位名稱 item['positionname'] = each.xpath("./td[1]/a/text()").extract()[0] # 詳情連線 item['positionlink'] = each.xpath("./td[1]/a/@href").extract()[0] # 職位類別 item['positionType'] = each.xpath("./td[2]/text()").extract()[0] # 招聘人數 item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0] # 工作地點 item['workLocation'] = each.xpath("./td[4]/text()").extract()[0] # 釋出時間 item['publishTime'] = each.xpath("./td[5]/text()").extract()[0] yield item if self.offset < 1680: self.offset += 10 # 每次處理完一頁的資料之後,重新發送下一頁頁面請求 # self.offset自增10,同時拼接為新的url,並呼叫回撥函式self.parse處理Response yield scrapy.Request(self.url + str(self.offset), callback = self.parse)
四、編寫pipelines檔案
# -*- coding: utf-8 -*- import json class TencentPipeline(object):
"""
功能:儲存item資料
""" def __init__(self): self.filename = open("tencent.json", "w") def process_item(self, item, spider): text = json.dumps(dict(item), ensure_ascii = False) + ",\n" self.filename.write(text.encode("utf-8")) return item def close_spider(self, spider): self.filename.close()
五、settings檔案設定(主要設定內容)
# 設定請求頭部,新增url DEFAULT_REQUEST_HEADERS = { "User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;", 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' } # 設定item——pipelines ITEM_PIPELINES = { 'tencent.pipelines.TencentPipeline': 300, }
執行命令,執行程式
# tencentPosition為爬蟲名 scrapy crwal tencentPosition
使用CrawlSpider類改寫
# 建立專案 scrapy startproject TencentSpider # 進入專案目錄下,建立爬蟲檔案 scrapy genspider -t crawl tencent tencent.com
item等檔案寫法不變,主要是爬蟲檔案的編寫
# -*- coding:utf-8 -*- import scrapy # 匯入CrawlSpider類和Rule from scrapy.spiders import CrawlSpider, Rule # 匯入連結規則匹配類,用來提取符合規則的連線 from scrapy.linkextractors import LinkExtractor from TencentSpider.items import TencentItem class TencentSpider(CrawlSpider): name = "tencent" allow_domains = ["hr.tencent.com"] start_urls = ["http://hr.tencent.com/position.php?&start=0#a"] # Response裡連結的提取規則,返回的符合匹配規則的連結匹配物件的列表 pagelink = LinkExtractor(allow=("start=\d+")) rules = [ # 獲取這個列表裡的連結,依次傳送請求,並且繼續跟進,呼叫指定回撥函式處理 Rule(pagelink, callback = "parseTencent", follow = True) ] # 指定的回撥函式 def parseTencent(self, response): for each in response.xpath("//tr[@class='even'] | //tr[@class='odd']"): item = TencentItem() # 職位名稱 item['positionname'] = each.xpath("./td[1]/a/text()").extract()[0] # 詳情連線 item['positionlink'] = each.xpath("./td[1]/a/@href").extract()[0] # 職位類別 item['positionType'] = each.xpath("./td[2]/text()").extract()[0] # 招聘人數 item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0] # 工作地點 item['workLocation'] = each.xpath("./td[4]/text()").extract()[0] # 釋出時間 item['publishTime'] = each.xpath("./td[5]/text()").extract()[0] yield item
奔跑 、拼搏、何時才能擁有你的懷抱