實戰(zhàn) | 手把手教你使用scrapy框架批量抓取招聘信息
回復“書籍”即可獲贈Python從入門到進階共10本電子書
爬前分析
爬取前我們來簡單分析一下騰訊的技術崗招聘網(wǎng)頁,進入網(wǎng)站并打開開發(fā)者工具,如下圖所示:



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
#第一頁
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
2、創(chuàng)建spider爬蟲
創(chuàng)建spider爬蟲的方式也很簡單,只要執(zhí)行如下命令即可:
scrapy genspider 爬取名 網(wǎng)站域名
scrapy genspider tencent careers.tencent.com
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學習群請在后臺回復【入群】
萬水千山總是情,點個【在看】行不行
/今日留言主題/
隨便說一兩句吧~~
