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,

其实就可以获得想得到的信息了。

猜你喜欢

转载自www.cnblogs.com/fudanxi/p/11980549.html