1. # 需求:获取腾讯招聘岗位信息前100页的内容,并保存至mongoDB数据库
    2. # 安装:pip install pymongo
    3. import requests,pymongo
    4. # 1.建立连接
    5. client = pymongo.MongoClient(host='127.0.0.1', port=27017) # host:主机号,port:连接的MongoDB的端口号
    6. # 2.进入数据库
    7. db = client['tencent'] # 如果有名为tencent的数据库就进入,没有就创建
    8. # 3.进入集合
    9. col = db['zhaopin']
    10. # 定义基础url
    11. base_url = 'https://careers.tencent.com/tencentcareer/api/post/Query'
    12. # 定义参数字典
    13. params = {
    14. 'pageSize': 10,
    15. 'language': 'zh-cn',
    16. 'area': 'cn'
    17. }
    18. # 定义请求头
    19. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36'}
    20. for page in range(1,6):
    21. params[ 'pageIndex'] = page
    22. # print('\n现在是第' +str(page) + '页============\n')
    23. # 发起请求,获取响应
    24. response = requests.get(url=base_url, headers=headers, params=params)
    25. Posts = response.json()['Data']['Posts']
    26. for post in Posts:
    27. dic = {}
    28. # 获取内容
    29. # 获取岗位内容
    30. RecruitPostName = post['RecruitPostName']
    31. # 获取招聘时间
    32. LastUpdateTime = post['LastUpdateTime']
    33. # 获取岗位要求
    34. Responsibility = post['Responsibility']
    35. # print(RecruitPostName, LastUpdateTime,Responsibility)
    36. dic['RecruitPostName'] = RecruitPostName
    37. dic['LastUpdateTime'] = LastUpdateTime
    38. dic['Responsibility'] = Responsibility
    39. # 4.插入数据
    40. col.insert(dic)
    41. # 5.关闭数据库
    42. client.close()
    43. # 在shell中查询保存到MongoDB的数据
    44. >>> mongo # 打开MongoDB
    45. >>> show dbs # 查看所有数据库
    46. >>> use tencent # 进入或创建数据库
    47. >>> show tables # 查看所有的聚合
    48. >>> db.zhaopin.find().pretty() #查询集合并格式化输出
    49. >>> it # 单次显示20多条数据,输入it查看更多