<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          實戰(zhàn) | 手把手教你使用scrapy框架批量抓取招聘信息

          共 6585字,需瀏覽 14分鐘

           ·

          2021-09-19 14:52

          點擊上方“Python爬蟲與數(shù)據(jù)挖掘”,進行關注

          回復“書籍”即可獲贈Python從入門到進階共10本電子書

          愿得此身長報國,何須生入玉門關。
          相信很多人都希望進騰訊這種大廠工作吧,工資高福利好,那么騰訊公司現(xiàn)在在招哪些職位?職位要求是什么呢?今天我們通過Scrapy框架來爬取騰訊招聘網(wǎng),一探究竟!

            爬前分析


          爬取前我們來簡單分析一下騰訊的技術崗招聘網(wǎng)頁,進入網(wǎng)站并打開開發(fā)者工具,如下圖所示:

          通過上圖可以知道,職位的數(shù)據(jù)存放在<div class="recruit-wrap recruit-margin"標簽中,那么我們打開網(wǎng)頁源代碼看看,數(shù)據(jù)是否存放在網(wǎng)頁源代碼里面,如下圖所示:
          發(fā)現(xiàn)數(shù)據(jù)沒有存放在源代碼里面,由此可以推斷數(shù)據(jù)是通過JavaScript渲染出來,那么我們查看Ajax請求,看看Ajax請求中有沒有存放職位信息的數(shù)據(jù),如下圖所示:
          既然有Ajax請求中存放著職位數(shù)據(jù),那么我們可以通過Ajax請求來獲取,先來看看該請求的URL鏈接是怎樣的:
          https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1629464904109&countryId=&cityId=&bgIds=&productId=&categoryId=40001001,40001002,40001003,40001004,40001005,40001006&parentCategoryId=&attrId=&keyword=&pageIndex=2&pageSize=10&language=zh-cn&area=cn
          發(fā)現(xiàn)該請求的URL那么長,那么我們嘗試刪減一下該URL的數(shù)據(jù),如下所示:
          #第一頁
          https://careers.tencent.com/tencentcareer/api/post/Query?categoryId=40001001,40001002,40001003,40001004,40001005,40001006&pageIndex=1&pageSize=10&language=zh-cn&area=cn

          #第二頁
          https://careers.tencent.com/tencentcareer/api/post/Query?categoryId=40001001,40001002,40001003,40001004,40001005,40001006&pageIndex=2&pageSize=10&language=zh-cn&area=cn

          #第三頁
          https://careers.tencent.com/tencentcareer/api/post/Query?categoryId=40001001,40001002,40001003,40001004,40001005,40001006&pageIndex=3&pageSize=10&language=zh-cn&area=cn

          通過簡單刪減可以得出該URL可以為上面的URL,而且pageIndex的翻頁的重要參數(shù)。

          好了,數(shù)據(jù)的存儲位置和URL已經(jīng)知道了,接下來我們開始爬取數(shù)據(jù)。


            實戰(zhàn)演練


          1、創(chuàng)建scrapy項目

          首先要創(chuàng)建一個scrapy項目,創(chuàng)建方式很簡單,只要在執(zhí)行以下命令即可:

          scrapy startproject Tencent
          執(zhí)行命令后,pycharm的項目目錄下會多了很多文件,如下圖所示:

          2、創(chuàng)建spider爬蟲

          創(chuàng)建spider爬蟲的方式也很簡單,只要執(zhí)行如下命令即可:

          scrapy genspider 爬取名 網(wǎng)站域名
          scrapy genspider tencent careers.tencent.com
          執(zhí)行后,在spiders文件夾中會多了一個tencent.py文件,該文件就是剛才創(chuàng)建的spider爬蟲,其文件內(nèi)容下所示:
          import scrapy

          class Tencent1Spider(scrapy.Spider):
              name = 'tencent'
              allowed_domains = ['careers.tencent.com']
              start_urls = ['http://careers.tencent.com/']

              def parse(self, response):
                  pass

          其中

          • name是我們的爬蟲名;

          • allowed_domains是域名,也就是爬蟲爬取的范圍;

          • start_urls是爬蟲最開始爬取的URL鏈接;

          • parse()是用來解析響應、提取數(shù)據(jù)。

          注意:parse()不能修改名字。

          3、定義字段

          在編寫代碼提取數(shù)據(jù)前,我們先來在items.py定義爬取的字段,字段類型為scrapy.Field,代碼如下所示:

          import scrapy

          class TencentItem(scrapy.Item):
              # define the fields for your item here like:
              RecruitPostName = scrapy.Field()    #職位名
              LocationName = scrapy.Field()        #地址
              Responsibility = scrapy.Field()        #工作要求


          4、提取數(shù)據(jù)

          定義好字段后,接下來開始在tencent.py中編寫代碼來提取數(shù)據(jù),具體代碼如下所示:

          import scrapy
          from Tencent.items import TencentItem
          class TencentSpider(scrapy.Spider):
              name = 'tencent'
              allowed_domains = ['careers.tencent.com']
              start_urls = ['https://careers.tencent.com/tencentcareer/api/post/Query?categoryId=40001001,40001002,40001003,40001004,40001005,40001006&pageIndex=1&pageSize=10&language=zh-cn&area=cn']

              def parse(self, response):
                  json=response.json()
                  datas = json.get('Data').get('Posts')
                  for data in datas:
                      item=TencentItem()
                      item['RecruitPostName']=data.get('RecruitPostName'),
                      item['LocationName']=data.get('LocationName'),
                      item['Responsibility']=data.get('Responsibility').replace('\n','')
                      yield item

          首先我們導入items.py文件中的TencentItem,再修改start_urls的URL鏈接,定義一個json變量來接收網(wǎng)頁響應的json()數(shù)據(jù),通過for循環(huán)把每條職位信息循環(huán)遍歷并提取我們想要的數(shù)據(jù)并放在item字典里面,其中item=TencentItem()相當于創(chuàng)建一個空字典item={},

          翻頁操作

          首頁我們已經(jīng)成功獲取到了,接下來要進行翻頁操作,具體代碼如下所示:

          for i in range(2,4):
              next_url=f'https://careers.tencent.com/tencentcareer/api/post/Query?categoryId=40001001,40001002,40001003,40001004,40001005,40001006&pageIndex={i}&pageSize=10&language=zh-cn&area=cn'
              yield scrapy.Request(
                  next_url,
                  callback=self.parse
                  )

          首先我們創(chuàng)建一個for循環(huán)來進行翻頁,調(diào)用yield生成器來返回數(shù)據(jù)給引擎,并調(diào)用scrapy.Request()方法,該方法能構建一個requests,同時指定提取數(shù)據(jù)的callback函數(shù)。

          5、settings.py配置

          在啟動爬取前,我們先要在settings.py文件中編寫一些代碼,具體代碼如下所示:

          LOG_LEVEL="WARNING"    
          ITEM_PIPELINES = {            
             'Tencent.pipelines.TencentPipeline'300,
          }
          USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'        

          其中:

          • LOG_LEVEL的作用的屏蔽log日志的輸出;

          • ITEM_PIPELINES的作用是開啟引擎;

          6、保存數(shù)據(jù)

          當我們要把數(shù)據(jù)保存成文件的時候,不需要任何額外的代碼,只要執(zhí)行如下代碼即可

          scrapy crawl 爬蟲名 -o xxx.json         #保存為JSON文件
          scrapy crawl 爬蟲名 -o xxx.csv              #保存為csv文件
          scrapy crawl 爬蟲名 -o xxx.xml             #保存為xml文件  

          當要把數(shù)據(jù)保存在數(shù)據(jù)庫里面或者txt文件時,則需要在pipelines.py文件中編寫代碼。

          好了,全部代碼已經(jīng)編寫好了,現(xiàn)在執(zhí)行以下命令來啟動爬蟲

          scrapy crawl tencent -o tencent.csv



            結果展示

          好了,爬取騰訊招聘就講到這里了,感謝觀看!

              小伙伴們,快快用實踐一下吧!如果在學習過程中,有遇到任何問題,歡迎加我好友,我拉你進Python學習交流群共同探討學習。

          ------------------- End -------------------

          往期精彩文章推薦:

          歡迎大家點贊,留言,轉發(fā),轉載,感謝大家的相伴與支持

          想加入Python學習群請在后臺回復【入群

          萬水千山總是情,點個【在看】行不行

          /今日留言主題/

          隨便說一兩句吧~~

          瀏覽 51
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  丁香五月天婷国产 | 日本黄色电影免费看网站 | 日韩三级大香蕉 | 99国产婷婷踪合在线免费视频 | 欧美日韩高清不卡 |