加入收藏 | 设为首页 | 会员中心 | 我要投稿 核心网 (https://www.hxwgxz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

Python数据可视化:浅谈数据分析岗

发布时间:2018-12-04 06:22:54 所属栏目:教程 来源:法纳斯特
导读:有态度地学习 讲道理,pyspider确实是一款优秀的爬虫框架,我们可以利用它快速方便地实现一个页面的抓

02 PyCharm获取拉勾网数据

  1. import requests 
  2. import pymysql 
  3. import random 
  4. import time 
  5. import json 
  6.  
  7. count = 0 
  8. # 设置请求网址及请求头参数 
  9. url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false' 
  10. headers = { 
  11.     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36', 
  12.     'Cookie': '你的Cookie值', 
  13.     'Accept': 'application/json, text/javascript, */*; q=0.01', 
  14.     'Connection': 'keep-alive', 
  15.     'Host': 'www.lagou.com', 
  16.     'Origin': 'https://www.lagou.com', 
  17.     'Referer': 'ttps://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90?labelWords=sug&fromSearch=true&suginput=shuju' 
  18.  
  19. # 连接数据库 
  20. db = pymysql.connect(host='127.0.0.1', user='root', password='774110919', port=3306, db='lagou_job', charset='utf8mb4') 
  21.  
  22.  
  23. def add_Mysql(id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare): 
  24.     # 将数据写入数据库中 
  25.     try: 
  26.         cursor = db.cursor() 
  27.         sql = 'insert into job(id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare) values ("%d", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")' % (id, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare); 
  28.         print(sql) 
  29.         cursor.execute(sql) 
  30.         print(cursor.lastrowid) 
  31.         db.commit() 
  32.     except Exception as e: 
  33.         print(e) 
  34.         db.rollback() 
  35.  
  36.  
  37. def get_message(): 
  38.     for i in range(1, 31): 
  39.         print('第' + str(i) + '页') 
  40.         time.sleep(random.randint(10, 20)) 
  41.         data = { 
  42.             'first': 'false', 
  43.             'pn': i, 
  44.             'kd': '数据分析' 
  45.         } 
  46.         response = requests.post(url=url, data=data, headers=headers) 
  47.         result = json.loads(response.text) 
  48.         job_messages = result['content']['positionResult']['result'] 
  49.         for job in job_messages: 
  50.             global count 
  51.             count += 1 
  52.             # 岗位名称 
  53.             job_title = job['positionName'] 
  54.             print(job_title) 
  55.             # 岗位薪水 
  56.             job_salary = job['salary'] 
  57.             print(job_salary) 
  58.             # 岗位地点 
  59.             job_city = job['city'] 
  60.             print(job_city) 
  61.             # 岗位经验 
  62.             job_experience = job['workYear'] 
  63.             print(job_experience) 
  64.             # 岗位学历 
  65.             job_education = job['education'] 
  66.             print(job_education) 
  67.             # 公司名称 
  68.             company_name = job['companyShortName'] 
  69.             print(company_name) 
  70.             # 公司类型 
  71.             company_type = job['industryField'] 
  72.             print(company_type) 
  73.             # 公司状态 
  74.             company_status = job['financeStage'] 
  75.             print(company_status) 
  76.             # 公司规模 
  77.             company_people = job['companySize'] 
  78.             print(company_people) 
  79.             # 工作技能 
  80.             if len(job['positionLables']) > 0: 
  81.                 job_tips = ','.join(job['positionLables']) 
  82.             else: 
  83.                 job_tips = 'None' 
  84.             print(job_tips) 
  85.             # 工作福利 
  86.             job_welfare = job['positionAdvantage'] 
  87.             print(job_welfare + 'nn') 
  88.             # 写入数据库 
  89.             add_Mysql(count, job_title, job_salary, job_city, job_experience, job_education, company_name, company_type, company_status, company_people, job_tips, job_welfare) 
  90.  
  91.  
  92. if __name__ == '__main__': 
  93.     get_message() 

(编辑:核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读