Python 爬取腾讯招聘职位详情 2019/12/4有效

我爬取的是Python相关职位,先po上代码,(PS:本人小白,这是跟着B站教学视频学习后,老师留的作业,因为腾讯招聘的网站变动比较大,老师的代码已经无法运行,所以po上),一些想法和过程在后面。

 from lxml import etree
import requests HEADERS = {
'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',
'Cookie': '__ga=GA1.2.212176558.1568885824; pgv_pvi=2298593280; _gcl_au=1.1.1370638257.1568885828; loading=agree',
'Referer': 'https://careers.tencent.com/search.html?keyword=python',
'Authority': 'careers.tencent.com',
"Dnt": ""
} #通过传入的indexNum获取Dict
def GetJsonByIndexUrl(indexNum):
base_url = "https://careers.tencent.com/tencentcareer/api/post/Query?timestamp=1575374831812&countryId=&cityId" \
"=&bgIds=&productId=&categoryId=&parentCategoryId=&attrId=&keyword=python&pageIndex={" \
"}&pageSize=10&language=zh-cn&area=cn"
url = base_url.format(indexNum) # 传入indexNum的值,构造出完整的indexURL
response = requests.get(url, headers=HEADERS)
postDict = response.json()
return postDict #通过获取的Dict取得每个职位的Id
def GetPostIdByDict(postDict):
postIds = []
data = postDict["Data"]
posts = data["Posts"]
for post in posts:
postId = post["PostId"]
postIds.append(postId)
return postIds #取得Id后,再获取职位详情内容
# post_url="https://careers.tencent.com/jobdesc.html?postId="这是详情页面,但是数据也是在json里面,所以直接获取json内容,
# 也就是下面的detail_url
def GetDetailByPostId(postIds):
detail_url = "https://careers.tencent.com/tencentcareer/api/post/ByPostId?timestamp=1575389747280&postId={}&language=zh-cn"
for id in postIds:
detail_url_byId = detail_url.format(id)
rsp = requests.get(detail_url_byId)
detailData = rsp.json()
print(detailData["Data"]) if __name__ == '__main__':
for x in range(1, 11): # 获取前10页的信息
mydict = GetJsonByIndexUrl(x)
postIds = GetPostIdByDict(mydict)
print("第", x, "页", "*" * 20)
GetDetailByPostId(postIds)
print("*" * 20)

一些想法和过程:

①一开始做的时候,发现职位的List不在当前页面,所以爬取这个无法获取信息,于是查看NetWork发现一个路径才是列表信息,

我命名为base_url,通过requests.get可以获得此List中的postId。

②点开一个职位的详情页面,发现其实详情内容也不在当前页面,内容又是一个新的路径,我命名为detail_url,通过requests.get,

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

上一篇:.net环境下跨进程、高频率读写数据


下一篇:[HNOI 2014]世界树