Python爬蟲(chóng)框架Scrapy實(shí)例代碼
目標(biāo)任務(wù):爬取騰訊社招信息,需要爬取的內(nèi)容為:職位名稱,職位的詳情鏈接,職位類別,招聘人數(shù),工作地點(diǎn),發(fā)布時(shí)間。
一、創(chuàng)建Scrapy項(xiàng)目
scrapy startproject Tencent
命令執(zhí)行后,會(huì)創(chuàng)建一個(gè)Tencent文件夾,結(jié)構(gòu)如下
二、編寫(xiě)item文件,根據(jù)需要爬取的內(nèi)容定義爬取字段
# -*- coding: utf-8 -*- import scrapy class TencentItem(scrapy.Item): # 職位名 positionname = scrapy.Field() # 詳情連接 positionlink = scrapy.Field() # 職位類別 positionType = scrapy.Field() # 招聘人數(shù) peopleNum = scrapy.Field() # 工作地點(diǎn) workLocation = scrapy.Field() # 發(fā)布時(shí)間 publishTime = scrapy.Field()
三、編寫(xiě)spider文件
進(jìn)入Tencent目錄,使用命令創(chuàng)建一個(gè)基礎(chǔ)爬蟲(chóng)類:
# tencentPostion為爬蟲(chóng)名,tencent.com為爬蟲(chóng)作用范圍 scrapy genspider tencentPostion "tencent.com"
執(zhí)行命令后會(huì)在spiders文件夾中創(chuàng)建一個(gè)tencentPostion.py的文件,現(xiàn)在開(kāi)始對(duì)其編寫(xiě):
# -*- coding: utf-8 -*- import scrapy from tencent.items import TencentItem class TencentpositionSpider(scrapy.Spider): """ 功能:爬取騰訊社招信息 """ # 爬蟲(chóng)名 name = "tencentPosition" # 爬蟲(chóng)作用范圍 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("http://tr[@class='even'] | //tr[@class='odd']"): # 初始化模型對(duì)象 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] # 招聘人數(shù) item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0] # 工作地點(diǎn) item['workLocation'] = each.xpath("./td[4]/text()").extract()[0] # 發(fā)布時(shí)間 item['publishTime'] = each.xpath("./td[5]/text()").extract()[0] yield item if self.offset < 1680: self.offset += 10 # 每次處理完一頁(yè)的數(shù)據(jù)之后,重新發(fā)送下一頁(yè)頁(yè)面請(qǐng)求 # self.offset自增10,同時(shí)拼接為新的url,并調(diào)用回調(diào)函數(shù)self.parse處理Response yield scrapy.Request(self.url + str(self.offset), callback = self.parse)
四、編寫(xiě)pipelines文件
# -*- coding: utf-8 -*- import json class TencentPipeline(object): """ 功能:保存item數(shù)據(jù) """ 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文件設(shè)置(主要設(shè)置內(nèi)容)
# 設(shè)置請(qǐng)求頭部,添加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' } # 設(shè)置item——pipelines ITEM_PIPELINES = { 'tencent.pipelines.TencentPipeline': 300, }
執(zhí)行命令,運(yùn)行程序
# tencentPosition為爬蟲(chóng)名 scrapy crwal tencentPosition
使用CrawlSpider類改寫(xiě)
# 創(chuàng)建項(xiàng)目 scrapy startproject TencentSpider # 進(jìn)入項(xiàng)目目錄下,創(chuàng)建爬蟲(chóng)文件 scrapy genspider -t crawl tencent tencent.com item等文件寫(xiě)法不變,主要是爬蟲(chóng)文件的編寫(xiě) # -*- coding:utf-8 -*- import scrapy # 導(dǎo)入CrawlSpider類和Rule from scrapy.spiders import CrawlSpider, Rule # 導(dǎo)入鏈接規(guī)則匹配類,用來(lái)提取符合規(guī)則的連接 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里鏈接的提取規(guī)則,返回的符合匹配規(guī)則的鏈接匹配對(duì)象的列表 pagelink = LinkExtractor(allow=("start=\d+")) rules = [ # 獲取這個(gè)列表里的鏈接,依次發(fā)送請(qǐng)求,并且繼續(xù)跟進(jìn),調(diào)用指定回調(diào)函數(shù)處理 Rule(pagelink, callback = "parseTencent", follow = True) ] # 指定的回調(diào)函數(shù) def parseTencent(self, response): for each in response.xpath("http://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] # 招聘人數(shù) item['peopleNum'] = each.xpath("./td[3]/text()").extract()[0] # 工作地點(diǎn) item['workLocation'] = each.xpath("./td[4]/text()").extract()[0] # 發(fā)布時(shí)間 item['publishTime'] = each.xpath("./td[5]/text()").extract()[0] yield item
總結(jié)
以上所述是小編給大家介紹的Python爬蟲(chóng)框架Scrapy實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!
相關(guān)文章
使用python+whoosh實(shí)現(xiàn)全文檢索
今天小編就為大家分享一篇使用python+whoosh實(shí)現(xiàn)全文檢索,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12簡(jiǎn)單了解python shutil模塊原理及使用方法
這篇文章主要介紹了簡(jiǎn)單了解python shutil模塊原理及使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04OpenCV-Python實(shí)現(xiàn)懷舊濾鏡與連環(huán)畫(huà)濾鏡
很多時(shí)候通過(guò)ps可以做很多效果,今天我們來(lái)介紹使用OpenCV-Python實(shí)現(xiàn)懷舊濾鏡與連環(huán)畫(huà)濾鏡,具有一定的參考價(jià)值,感興趣的可以了解一下2021-06-06Python 使用 PyMysql、DBUtils 創(chuàng)建連接池提升性能
DBUtils 是一套 Python 數(shù)據(jù)庫(kù)連接池包,并允許對(duì)非線程安全的數(shù)據(jù)庫(kù)接口進(jìn)行線程安全包裝。這篇文章主要介紹了Python 使用 PyMysql、DBUtils 創(chuàng)建連接池,提升性能,需要的朋友可以參考下2019-08-08關(guān)于tf.reverse_sequence()簡(jiǎn)述
今天小編就為大家分享一篇關(guān)于tf.reverse_sequence()簡(jiǎn)述,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01對(duì)python3 一組數(shù)值的歸一化處理方法詳解
今天小編就為大家分享一篇對(duì)python3 一組數(shù)值的歸一化處理方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能實(shí)例
這篇文章主要介紹了Python爬蟲(chóng)實(shí)現(xiàn)爬取百度百科詞條功能,結(jié)合完整實(shí)例形式分析了Python爬蟲(chóng)的基本原理及爬取百度百科詞條的步驟、網(wǎng)頁(yè)下載、解析、數(shù)據(jù)輸出等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04python實(shí)戰(zhàn)之90行代碼寫(xiě)個(gè)猜數(shù)字游戲
這篇文章主要介紹了python實(shí)戰(zhàn)之90行代碼寫(xiě)個(gè)猜數(shù)字,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們有很大的幫助,需要的朋友可以參考下2021-04-04pytorch模型保存到本地后,如何實(shí)現(xiàn)繼續(xù)訓(xùn)練
在PyTorch中,保存和加載模型對(duì)于實(shí)現(xiàn)模型訓(xùn)練的中斷和恢復(fù)非常有用,保存模型主要有兩種方式:一是保存整個(gè)模型包括結(jié)構(gòu)與參數(shù);二是僅保存模型的state_dict,加載模型時(shí),若保存了整個(gè)模型則直接加載,若僅保存了state_dict,則需先實(shí)例化模型結(jié)構(gòu)后加載2024-09-09python3使用requests模塊爬取頁(yè)面內(nèi)容的實(shí)戰(zhàn)演練
本篇文章主要介紹了python3使用requests模塊爬取頁(yè)面內(nèi)容的實(shí)戰(zhàn)演練,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09