查看: 3789| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

Web scraping and exploratory analysis of data scientist job ads

🔗
DL | 只看该作者 |倒序浏览
全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 DL 于 2017-6-10 23:51 编辑 . 1point 3 acres

贴些招工广告的抓数据程序和分析结果,希望对大家有用,也起个抛砖引玉的作用。. .и
1. Web scraping of kaggle jobs
  1. # -*- coding: utf-8 -*-
  2. from urllib.request import urlopen
  3. from urllib.error import HTTPError
  4. import re
  5. import string. 1point3acres
  6. from bs4 import BeautifulSoup ..
  7. import pymysql
  8. import nltk
  9. from nltk.corpus import stopwords
  10. from time import sleep # To prevent overwhelming the server

  11. stop_words = set(stopwords.words('english')). Waral dи,
  12. stop_words.update(set(string.punctuation)).google  и
  13. # Open database connection
  14. db = pymysql.connect("localhost", user = "testuser", passwd = "******",
  15.                      db="jobs", charset = 'utf8')
  16. # prepare a cursor object using cursor() method
  17. cursor = db.cursor()

  18. .google  и
  19. # execute SQL query using execute() method.. 1point 3 acres
  20. #cursor.execute('''DROP TABLE IF EXISTS kaggle'''). Waral dи,
  21. cursor.execute ('''  CREATE TABLE  IF NOT EXISTS kaggle  
  22.     (id INT NOT NULL, Company VARCHAR(150), Title VARCHAR(150),
  23.     Location VARCHAR(150), Contents VARCHAR(3000),
  24.     Created VARCHAR(20), Reviews INT, Link VARCHAR(50),
  25.     PRIMARY KEY(id)) ''')
  26. . .и
  27. cursor.execute(''' SELECT id FROM kaggle ''')
  28. data = cursor.fetchall()
  29. jobid_visited = set([d[0] for d in data])

  30. jobId_start = 16800. Χ
  31. jobId_end = 17900

  32. home_url = "https://www.kaggle.com/jobs/".--
  33. ..
  34. for job_id in range(jobId_start, jobId_end):
  35.     if job_id % 50 == 0:. From 1point 3acres bbs
  36.         print(job_id).--
  37.         sleep(1)
  38.     if job_id in jobid_visited:
  39.         continue. check 1point3acres for more.
  40.     job_link = "%s%s" % (home_url, job_id). 1point 3acres
  41.     try:
  42.         html = urlopen(job_link)
  43.     except HTTPError as e:
    . Χ
  44.         print(job_id, e)
  45.         continue
  46.     soup = BeautifulSoup(html.read(), "lxml")
  47.     job_title = soup.find('div', attrs={'class':'title'})
  48.     title = job_title.h1.getText()
  49.     company = job_title.h2.getText()
  50.     location = job_title.h3.getText()
  51.     submission = soup.find('p', attrs={'class':'submission-date'})
  52.     submission_date = submission.span['title'].split()[0]
  53.     reviews = submission.contents[2]
  54.     reviews = int(''.join(list(filter(str.isdigit, reviews)))). 1point 3 acres
  55.    
  56.     contents = submission.next_siblings # job descriptions tag
  57.     word_set = set()
  58.     for para in contents:
  59.         if para == '\n':. .и
  60.             continue      

  61.         if isinstance(para, str):
  62.             text = para.strip().--
  63.         else:
  64.             text = para.get_text().strip()
  65.             
  66.         text = re.sub(r'[^\x00-\x7f]', r'', text) # remove non ASCII
  67.         words = nltk.tokenize.word_tokenize(text.lower())
  68.         words = [w for w in words if ((not w in stop_words) \
  69.                     and (not "'" in w))]
  70.         word_set.update(words). 1point 3 acres
  71.                                             
  72.     text = ' '.join(word_set)
  73.     if len(text) > 3000:
  74.         text = text[:3000]
  75.     . Χ
  76.     company = re.sub(r'[\'\"]', r' ', company)
  77.     location = re.sub(r'[\'\"]', r' ', location)
  78.     title = re.sub(r'[\'\"]', r' ', title)
  79.    
  80.     if len(company) > 150:. Waral dи,
  81.         company = company[:150]
  82.         . 1point 3acres
  83.     if len(title) > 150:.1point3acres
  84.         title = title[:150]. 1point 3acres
  85.         
  86.     if len(location) > 150:
  87.         location = location[:150]
  88.    
  89.     sql = '''INSERT INTO kaggle
  90.             (id, Company, Title, Location, Contents, Created, Reviews, Link)
  91.             VALUES ('%d', '%s', '%s', '%s', '%s', '%s', '%d', '%s')''' %\
  92.             (job_id, company, title, location, text,
  93.              submission_date, reviews, job_link)
  94.     try:
  95.         cursor.execute(sql).google  и
  96.         cursor.connection.commit()
  97.     except Exception as e:
  98.         print(job_id, e) ..
  99.         break

  100. # Fetch a single row using fetchone() method.. 1point3acres.com
  101. cursor.execute(''' SELECT * FROM kaggle ''')
  102. data = cursor.fetchone()
  103. print (data)
  104.    
  105. # disconnect from server ..
  106. cursor.close()
  107. db.close()

复制代码
2. Web scraping of careerbuilder
  1. # -*- coding: utf-8 -*. 1point 3acres
复制代码
indeed.com 的 web scraping code 见 Jesse's blog. https://jessesw.com/Data-Science-Skills/
关键词分析 code 也见 https://jessesw.com/Data-Science-Skills/

主要参考:
1. "Web Scraping with Python" by Ryan Mitchell
2.  "Data Science in R: A Case Studies Approach to Computational Reasoning and Problem solving" Ch 12. Exploring data science jobs with web scraping and text minin
3. https://jessesw.com/Data-Science-Skills/

评分

参与人数 4大米 +186 收起 理由
湖畔飞燕 + 1 感谢分享!
anonym + 150
dawei756 + 10 很有用的信息!
duanmupeiyi + 25 感谢分享!

查看全部评分


上一篇:UIUC online DS vs GT OMCS DS 求比较
下一篇:统计小硕自学DS打卡贴
🔗
 楼主| DL 2017-6-10 23:56:50 | 只看该作者
全局:
web scraping careerbuilder
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. from urllib.request import urlopen
  4. from urllib.error import HTTPError
  5. import re
  6. import datetime
  7. import string
  8. from bs4 import BeautifulSoup
  9. import pymysql
  10. import nltk
  11. from nltk.corpus import stopwords. From 1point 3acres bbs
  12. from time import sleep # To prevent overwhelming the server ..
  13. . 1point 3acres
  14. date_today = datetime.date.today()

  15. stop_words = set(stopwords.words('english'))
  16. stop_words.update(set(string.punctuation))

  17. # Open database connection
  18. db = pymysql.connect("localhost", user = "testuser", passwd = "******",
  19.                      db="jobs", charset = 'utf8')
  20. # prepare a cursor object using cursor() method.--
  21. cursor = db.cursor()

  22. # execute SQL query using execute() method.
  23. #cursor.execute('''DROP TABLE IF EXISTS careerBuilder'''). 1point3acres
  24. cursor.execute ('''  CREATE TABLE  IF NOT EXISTS careerBuilder  
  25.     (id VARCHAR(50) NOT NULL, Company VARCHAR(150), Title VARCHAR(150), . Χ
  26.     Location VARCHAR(150), Contents VARCHAR(3000),
  27.     Industry VARCHAR(150), Category VARCHAR(150),
  28.     Link VARCHAR(200), Salary VARCHAR(50), Created DATE,
  29.     PRIMARY KEY(id)) ''')

  30. cursor.execute(''' SELECT id FROM careerBuilder ''')
  31. data = cursor.fetchall()
  32. jobid_visited = set([d[0] for d in data]).1point3acres

  33. ds_url = "https://www.careerbuilder.com/jobs-data-scientist"
  34. sort_by = "date_desc"
  35. page = 1-baidu 1point3acres
  36. url = "%s?page_number=%d&sort=%s" % (ds_url, page, sort_by)
  37. try:
  38.     html = urlopen(url)
  39. except HTTPError as e:. Waral dи,
  40.     print(e)
  41.     sys.exit()

  42. soup = BeautifulSoup(html.read(), "lxml")

  43. num_pages = soup.find('span', attrs = {'class':'page-count'}).get_text(). Χ
  44. num_pages = int(re.findall(r'\d+', num_pages)[1])
  45. .google  и
  46. base_url = "https://www.careerbuilder.com"
  47. #for page in range(1, num_pages + 1):
  48. for page in range(1, num_pages + 1):
  49.     print('start page: ', page). 1point3acres.com
  50.     url = "%s?page_number=%d&sort=%s" % (ds_url, page, sort_by)
  51.     try:
  52.         html = urlopen(url)
  53.     except HTTPError as e:
  54.         print(page, e)
  55.         break

  56.     soup = BeautifulSoup(html.read(), "lxml")
  57.     jobs = soup.findAll('h2', attrs={'class':'job-title'}). From 1point 3acres bbs
  58.     for job in jobs:
  59.         # retriev job id
  60.         job_did = job.a.get('data-job-did')
  61.         if job_did in jobid_visited:
  62.             continue.1point3acres
  63.         
  64.         job_link = base_url + job.a.get('href')        
  65.         try:
  66.             job_soup = BeautifulSoup(urlopen(job_link).read(), "lxml")
  67.         except Exception as e:
  68.             print(page, job_did, e). .и
  69.             continue
  70.             
  71.         job_detail = job_soup.find('div', attrs={'class':'card with-padding'}). .и
  72.         title = job_detail.h1.get_text().strip()
  73.         company_location = job_detail.h2.get_text().strip().split('\n')
  74.         if len(company_location) == 1:
  75.             location = company_location[0]
  76.         elif len(company_location) == 3:
  77.             company, location = company_location[0], company_location[2]. 1point 3acres
  78.         
  79.         # retriev job posted date
  80.         begin_date = job_detail.h3.get_text().strip()
  81.         begin_date = re.findall('(\d+) day', begin_date)
  82.         if len(begin_date) == 0 :. 1point 3acres
  83.             time_delta = 0
  84.         else :. 1point 3acres
  85.             time_delta = int(begin_date[0])
  86.         
  87.         begin_date = date_today - datetime.timedelta(days=time_delta)
  88.         begin_date = begin_date.strftime("%y/%m/%d")
  89.         
  90.         # retriev job category. Waral dи,
  91.         snapshot = job_detail.find('div', attrs={'class':'job-facts item'})
  92.         
  93.         job_industry = snapshot.find('div', id='job-industry')
  94.         if job_industry:
  95.             job_industry = job_industry.get_text().strip(). 1point 3 acres
  96.         
  97.         job_category = snapshot.find('div', id='job-categories')
  98.         if job_category:
  99.             job_category = job_category.get_text().strip(). 1point 3acres
  100.         
  101.         # find anually salary if available. 1point3acres
  102.         salary = ''. check 1point3acres for more.
  103.         for line in snapshot.get_text().splitlines():
  104.             salary_entry = re.findall(r'^(\$.*)/Year
  105. -baidu 1point3acres

  106. , line)
  107.             if len(salary_entry) == 1:
  108.                 salary = salary_entry[0].strip()
  109.                
  110.         job_id = job_detail.find('div', class_='small-12 columns job-id')
  111.         if job_id:
  112.             job_id = job_id.get_text().strip().split('\n')[1]
  113.         
  114.         # get job description and requirements
  115.         job_item = job_detail.find('div', class_='small-12 columns item')
  116.         text = job_item.get_text()
  117.         # break into lines ..
  118.         lines = (line.strip() for line in text.splitlines())
  119.         # break multi-headlines into a line each
  120.         chunks = (phrase.strip() for line in lines
  121.             for phrase in line.split(" "))
  122.    
  123.         text = ' '.join(chunk for chunk in chunks if chunk)
  124.         text = re.sub(r"[\'\-\/&]", r' ', text)
  125.         words = nltk.tokenize.word_tokenize(text.lower()). ----
  126.         words = [w for w in words if (not w in stop_words)]
  127.         word_set = set(words)
  128.         . 1point 3acres
  129.         text = ' '.join(word_set). From 1point 3acres bbs

  130.         if len(text) > 3000:. From 1point 3acres bbs
  131.             text = text[:3000]
  132.         
  133.         company = re.sub(r'[\'\"]', r' ', company)
  134.         location = re.sub(r'[\'\"]', r' ', location)
  135.         title = re.sub(r'[\'\"]', r' ', title). Waral dи,
  136.         
  137.         if len(company) > 150:
  138.             company = company[:150].1point3acres
  139.             
  140.         if len(title) > 150:
  141.             title = title[:150]
  142.             
  143.         if len(location) > 150:. check 1point3acres for more.
  144.             location = location[:150]
  145.             
  146. #        print(job_did, title, company, location, salary, begin_date, . Waral dи,
  147. #              job_industry, job_category, job_link, text)
  148. #        print("------------------------------------------")
  149.         
  150.         sql = '''INSERT INTO careerBuilder
  151.                 (id, Company, Title, Location, Contents, Industry,
  152.                 Category, Link, Salary, Created)
  153.                 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', . 1point3acres
  154.                 '%s', '%s', STR_TO_DATE('%s', '%%y/%%m/%%d'))''' %\.
  155.                 (job_did, company, title, location, text,
  156.                   job_industry, job_category,
  157.                  job_link, salary, begin_date)
  158.         try:
  159.             cursor.execute(sql).1point3acres
  160.             cursor.connection.commit()
  161.         except Exception as e:. 1point 3 acres
  162.             print(job_id, e). Χ
  163.             break
  164.    
  165.     sleep(1)

  166. # Fetch a single row using fetchone() method.
  167. cursor.execute(''' SELECT * FROM careerBuilder ''')
  168. data = cursor.fetchone(). Χ
  169. print (data) -baidu 1point3acres
  170.    
  171. # disconnect from server
  172. cursor.close()
  173. db.close()
    . Waral dи,

复制代码
回复

使用道具 举报

🔗
 楼主| DL 2017-6-11 00:03:35 | 只看该作者
全局:
本帖最后由 DL 于 2017-6-11 00:35 编辑

kaggle上jobs的关键技能排序 和 educatation 要求

  • kaggle 列的job更接近data scientist/engineer。
  • 和书中以前的数据比,Python, machine learning, Spark, Deep learning, cloud 占的比例在上升。
  • PhD 更受欢迎 。. ----

更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
 楼主| DL 2017-6-11 00:13:40 | 只看该作者
全局:
career builder 上jobs的关键技能排序 和 educatation 要求


更多图片 小图 大图
组图打开中,请稍候......
回复

使用道具 举报

🔗
 楼主| DL 2017-6-14 21:22:20 | 只看该作者
全局:
career builder 上按data scientist关键词搜到的工作(其中会包含一些data analyst的工作)以industry排序

careerbuilder_industry.png (28.89 KB, 下载次数: 31)

careerbuilder_industry.png
回复

使用道具 举报

🔗
jessicalmj 2017-6-15 02:02:19 | 只看该作者
全局:
这个挺酷的,看来每个网站的侧重还是不一样的。楼主有分析其他网站吗?Glassdoor, indeed 什么的
回复

使用道具 举报

🔗
 楼主| DL 2017-6-23 10:43:49 | 只看该作者
全局:
jessicalmj 发表于 2017-6-15 02:02
这个挺酷的,看来每个网站的侧重还是不一样的。楼主有分析其他网站吗?Glassdoor, indeed 什么的

indeed 的工作分析 https://jessesw.com/Data-Science-Skills/ 上有。我没有具体搞。 结果应该和CareerBuilder比较接近。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表