Python 爬取騰訊招聘職位詳情 2019/12/4有效


我爬取的是Python相關職位,先po上代碼,(PS:本人小白,這是跟着B站教學視頻學習后,老師留的作業,因為騰訊招聘的網站變動比較大,老師的代碼已經無法運行,所以po上),一些想法和過程在后面。

 1 from lxml import etree
 2 import requests
 3 
 4 HEADERS = {
 5     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.36',
 6     'Cookie': '__ga=GA1.2.212176558.1568885824; pgv_pvi=2298593280; _gcl_au=1.1.1370638257.1568885828; loading=agree',
 7     'Referer': 'https://careers.tencent.com/search.html?keyword=python',
 8     'Authority': 'careers.tencent.com',
 9     "Dnt": "1"
10 }
11 
12 
13 #通過傳入的indexNum獲取Dict
14 def GetJsonByIndexUrl(indexNum):
15     base_url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1575374831812&countryId=&cityId" \
16                "=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=python&pageIndex={" \
17                "}&pageSize=10&language=zh-cn&area=cn"
18     url = base_url.format(indexNum)  # 傳入indexNum的值,構造出完整的indexURL
19     response = requests.get(url, headers=HEADERS)
20     postDict = response.json()
21     return postDict
22 
23 #通過獲取的Dict取得每個職位的Id
24 def GetPostIdByDict(postDict):
25     postIds = []
26     data = postDict["Data"]
27     posts = data["Posts"]
28     for post in posts:
29         postId = post["PostId"]
30         postIds.append(postId)
31     return postIds
32 
33 #取得Id后,再獲取職位詳情內容
34 # post_url="https://careers.tencent.com/jobdesc.html?postId="這是詳情頁面,但是數據也是在json里面,所以直接獲取json內容,
35 # 也就是下面的detail_url
36 def GetDetailByPostId(postIds):
37     detail_url = "https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp=1575389747280&postId={}&language=zh-cn"
38     for id in postIds:
39         detail_url_byId = detail_url.format(id)
40         rsp = requests.get(detail_url_byId)
41         detailData = rsp.json()
42         print(detailData["Data"])
43 
44 
45 if __name__ == '__main__':
46     for x in range(1, 11):  # 獲取前10頁的信息
47         mydict = GetJsonByIndexUrl(x)
48         postIds = GetPostIdByDict(mydict)
49         print("", x, "", "*" * 20)
50         GetDetailByPostId(postIds)
51         print("*" * 20)

一些想法和過程:

①一開始做的時候,發現職位的List不在當前頁面,所以爬取這個無法獲取信息,於是查看NetWork發現一個路徑才是列表信息,

我命名為base_url,通過requests.get可以獲得此List中的postId。

②點開一個職位的詳情頁面,發現其實詳情內容也不在當前頁面,內容又是一個新的路徑,我命名為detail_url,通過requests.get,

其實就可以獲得想得到的信息了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM