Advertisement
Guest User

indeed

a guest
Dec 4th, 2017
1,488
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.87 KB | None | 0 0
  1.  
  2. # coding: utf-8
  3.  
  4. # In[1]:
  5.  
  6.  
  7. login_url = 'https://secure.indeed.com/account/login'
  8.  
  9.  
  10. # In[2]:
  11.  
  12.  
  13. data = {
  14.         'action':'login',
  15.         '__email':'anonymous@gmail.com',
  16.         '__password':'password',
  17.         'remember':'1',
  18.         'hl':'en',
  19.         'continue':'/account/view?hl=en',
  20.        }
  21.  
  22.  
  23. # In[3]:
  24.  
  25.  
  26. header = {'User_Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'}
  27.  
  28.  
  29. # In[4]:
  30.  
  31.  
  32. def listhandler(lst):
  33.     b=[]
  34.     for i in range(0,5):
  35.         try:
  36.             b.append(lst[i])
  37.         except:
  38.             b.append('')
  39.     return b
  40.  
  41.  
  42. # In[5]:
  43.  
  44.  
  45. def ingester(string):
  46.     from bs4 import BeautifulSoup
  47.     import requests
  48.     from datetime import datetime
  49.     import re
  50.     import time
  51.     from elasticsearch import Elasticsearch
  52.     es = Elasticsearch()
  53.     #start session s
  54.     s = requests.session()
  55.     #login with login and password
  56.     r = s.post(login_url,data = data)
  57.     #get resume page1
  58.     rq= s.get(string)
  59.     url = []
  60.     soup = ''
  61.     all_content=[]
  62.     soup = BeautifulSoup(rq.text,'html.parser')
  63.     content = soup.find_all('a', class_= 'app_link')
  64.     #get urls in a page
  65.     for c in content:
  66.         full = 'https://www.indeed.com'+c.attrs['href']
  67.         url.append(full)
  68.     cnt = len(content)
  69.     track = 0
  70.     #open each url and look for contents
  71.     for j in range(0,cnt):
  72.         track += 1
  73.         r = s.get(url[j],headers=header)
  74.         soup = BeautifulSoup(r.text,'html.parser')
  75.         #Resume name
  76.         name = soup.find('h1').text
  77.         name = name.title()
  78.         print(name)
  79.         #experience
  80.         experiences = soup.find_all('div',class_=re.compile(r'work-experience-section( \w)*'))
  81.         experience_list = []
  82.         company_list=[]
  83.         YOE_list = []
  84.         jobtitle_list=[]
  85.         location_c_list=[]
  86.         if len(experiences) > 5:
  87.             experiences = [experiences[0],experiences[1],experiences[2],experiences[3],experiences[4]]
  88.         for experience in experiences:
  89.             if experience.find('p',class_='work_title title') is None:
  90.                 jobtitle = 'No Job Title'
  91.             else:
  92.                 jobtitle = experience.find('p',class_='work_title title').text
  93.             if experience.find('div',class_='work_company') is None:
  94.                 comp_lo = 'No Company'
  95.             else:
  96.                 comp_lo = experience.find('div',class_='work_company').text.split(' - ')
  97.             if len(comp_lo) == 2:
  98.                 company,location_c = experience.find('div',class_='work_company').text.split(' - ')
  99.             elif len(comp_lo) ==1 :
  100.                 company = experience.find('div',class_='work_company').text
  101.                 location_c = 'No Company Location'
  102.             else:
  103.                 company = 'No Company'
  104.             if experience.find('p',class_='work_dates') is not None:
  105.                 YOE = 0
  106.                 period = experience.find('p',class_='work_dates').text.split(' to ')
  107.                 #print (period)   # AA BB 1, AA PRESENT 2 A PRESENT 3 A BB 4 AA B 5
  108.                 if len(period) == 2:
  109.                     if period[1] == 'Present':
  110.                         try:
  111.                             period[1] = datetime.today().strftime('%B %Y')
  112.                         except:
  113.                             period[1] = datetime.today().strftime('%b %Y')
  114.                     if len(period[0]) > 4 and len(period[1]) > 4:
  115.                         #type AA BB
  116.                         try:
  117.                             prd = (datetime.strptime(period[1],'%B %Y').date()-                            datetime.strptime(period[0],'%B %Y').date())
  118.                         except:
  119.                             prd = (datetime.strptime(period[1],'%b %Y').date()-                            datetime.strptime(period[0],'%b %Y').date())
  120.                    
  121.                         YOE = int(abs(round(prd.days/365,0)))
  122.                     elif len(period[0]) > 4 and len(period[1]) < 5:
  123.                         #type AA B
  124.                         YOE = int(period[1])-int(period[0].split()[1])
  125.                     elif len(period[0]) < 5 and len(period[1]) > 4:
  126.                         #type A BB
  127.                         YOE = int(period[1].split()[1])-int(period[0])
  128.                     elif len(period[0]) < 5 and len(period[1]) < 5:
  129.                         #type A B
  130.                         YOE = int(period[1])-int(period[0])
  131.                 else:
  132.                     YOE = 1
  133.                 if YOE < 1:
  134.                     YOE = 0
  135.             else:
  136.                 YOE = None
  137.             jobdesc = experience.find('p',class_='work_description')
  138.             if jobdesc is None:
  139.                 desc = 'No job description'
  140.             else:
  141.                 desc = jobdesc.text
  142.             #print(jobtitle,'\n',company,location_c,'\n',desc,'\n',YOE,'year')
  143.             company_list.append(company)
  144.             YOE_list.append(YOE)
  145.             experience_list.append(desc)
  146.             jobtitle_list.append(jobtitle)
  147.             location_c_list.append(location_c)
  148.         #break
  149.             #education education-section last
  150.         educations = soup.find_all('div',class_=re.compile(r'education-section( \w)*'))
  151.         school_list=[]
  152.         YOEdu_list = []
  153.         degree_list=[]
  154.         location_list = []
  155.         if len(educations) > 5:
  156.             educations = [educations[0],educations[1],educations[2],educations[3],educations[4]]
  157.         for e in educations:
  158.             if e.find('p', class_ = 'edu_title') is None:
  159.                 degree = 'No Education Information'
  160.             else:
  161.                 degree = e.find('p', class_ = 'edu_title').text
  162.             if e.find('div', class_='edu_school') is None:
  163.                 edu_school = 'No School Information'
  164.             else:
  165.                 edu_school = e.find('div',class_='edu_school').text.split(' - ')[0]
  166.             if e.find('div',class_='inline-block') is None:
  167.                 edu_location = 'No Education Location Info'
  168.             else:
  169.                 edu_location = e.find('div',class_='inline-block').text
  170.             if e.find('p',class_='edu_dates') is not None:
  171.                 YOEdu = 0
  172.                 edu_period = e.find('p',class_='edu_dates').text.split(' to ')
  173.                 print (edu_period)   # AA BB 1, AA PRESENT 2 A PRESENT 3 A BB 4 AA B 5
  174.                 if len(edu_period) == 2:
  175.                     if edu_period[1] == 'Present':
  176.                         try:
  177.                             edu_period[1] = datetime.today().strftime('%B %Y')
  178.                         except:
  179.                             edu_period[1] = datetime.today().strftime('%b %Y')
  180.                     if len(edu_period[0]) > 4 and len(edu_period[1]) > 4:
  181.                         #type AA BB
  182.                         try:
  183.                             edu_prd = (datetime.strptime(edu_period[1],'%B %Y').date()-                            datetime.strptime(edu_period[0],'%B %Y').date())
  184.                         except:
  185.                             edu_prd = (datetime.strptime(edu_period[1],'%b %Y').date()-                            datetime.strptime(period[0],'%b %Y').date())
  186.                    
  187.                         YOEdu = int(abs(round(edu_prd.days/365,0)))
  188.                     elif len(edu_period[0]) > 4 and len(edu_period[1]) < 5:
  189.                         #type AA B
  190.                         YOEdu = int(edu_period[1])-int(edu_period[0].split()[1])
  191.                     elif len(edu_period[0]) < 5 and len(edu_period[1]) > 4:
  192.                         #type A BB
  193.                         YOEdu = int(edu_period[1].split()[1])-int(edu_period[0])
  194.                     elif len(edu_period[0]) < 5 and len(edu_period[1]) < 5:
  195.                         #type A B
  196.                         YOEdu = int(edu_period[1])-int(edu_period[0])
  197.                 else:
  198.                     YOEdu = 1
  199.                 if YOEdu < 1:
  200.                     YOEdu = 0
  201.             else:
  202.                 YOEdu = None
  203.             #print(degree,'\n',edu_school,edu_location,'\n',YOEdu,'year')
  204.             school_list.append(edu_school)
  205.             YOEdu_list.append(YOEdu)
  206.             degree_list.append(degree)
  207.             location_list.append(edu_location)
  208.        
  209.             #Skills
  210.         skill_set = soup.find_all('span',class_='skill-text')
  211.         skills=''
  212.         for sk in skill_set:
  213.             skills += (sk.text)
  214.         #print(skills)
  215.             #Certifications/Licenses
  216.    
  217.         cert = soup.find_all('div',class_=re.compile(r'certification-section( \w)*'))
  218.         certs = ''
  219.         for c in cert:
  220.             certs += (c.text)
  221.         #prepare list
  222.         company_list = listhandler(company_list)
  223.         YOE_list = listhandler(YOE_list)
  224.         experience_list = listhandler(experience_list)
  225.         jobtitle_list = listhandler(jobtitle_list)
  226.         location_c_list = listhandler(location_c_list)
  227.         school_list = listhandler(school_list)
  228.         YOEdu_list = listhandler(YOEdu_list)
  229.         degree_list = listhandler(degree_list)
  230.         location_list = listhandler(location_list)
  231.         es.index(index='resume',
  232.                     doc_type='test-type',
  233.                     body={'id': track,
  234.                         'applicant':name,
  235.                          "experience": {
  236.                              "company_1":company_list[0],
  237.                              "title_1":jobtitle_list[0],
  238.                              "years of experience_1":YOE_list[0],
  239.                              "location_1":location_c_list[0],
  240.                              "description_1":experience_list[0],
  241.                              "company_2":company_list[1],
  242.                              "title_2":jobtitle_list[1],
  243.                              "years of experience_2":YOE_list[1],
  244.                              "location_2":location_c_list[1],
  245.                              "description_2":experience_list[1],
  246.                              "company_3":company_list[2],
  247.                              "title_3":jobtitle_list[2],
  248.                              "years of experience_3":YOE_list[2],
  249.                              "location_3":location_c_list[2],
  250.                              "description_3":experience_list[2],
  251.                              "company_4":company_list[3],
  252.                              "title_1":jobtitle_list[3],
  253.                              "years of experience_4":YOE_list[3],
  254.                              "location_4":location_c_list[3],
  255.                              "description_4":experience_list[3],
  256.                              "company_5":company_list[4],
  257.                              "title_5":jobtitle_list[4],
  258.                              "years of experience_5":YOE_list[4],
  259.                              "location_5":location_c_list[4],
  260.                              "description_5":experience_list[4]
  261.                         },
  262.                         "education":{
  263.                             "school_1":school_list[0],
  264.                             "degree_1":degree_list[0],
  265.                             "location_1":location_list[0],
  266.                             "yearsOfeducation_1":YOEdu_list[0],
  267.                             "school_2":school_list[1],
  268.                             "degree_2":degree_list[1],
  269.                             "location_2":location_list[1],
  270.                             "yearsOfeducation_2":YOEdu_list[1],
  271.                             "school_3":school_list[2],
  272.                             "degree_3":degree_list[2],
  273.                             "location_3":location_list[2],
  274.                             "yearsOfeducation_3":YOEdu_list[2],
  275.                             "school_4":school_list[3],
  276.                             "degree_4":degree_list[3],
  277.                             "location_4":location_list[3],
  278.                             "yearsOfeducation_4":YOEdu_list[3],
  279.                             "school_5":school_list[4],
  280.                             "degree_5":degree_list[4],
  281.                             "location_5":location_list[4],
  282.                             "yearsOfeducation_5":YOEdu_list[4]
  283.                         },
  284.                         "skills":skills,
  285.                         "certifications":certs})
  286.         time.sleep(2)
  287.     return True
  288.         #break
  289.             #Adwards
  290.        
  291.             #Additional Information
  292.  
  293.  
  294. # In[6]:
  295.  
  296.  
  297. url_base = 'https://www.indeed.com/resumes?q=data+scientist&l=GTA%2C+ON&co=CA&cb=jt&start='
  298. for i in range(0,6):
  299.     print('working on page: %d' %(i+1))
  300.     ingester(url_base+str(i*50))
  301.    
  302.  
  303.  
  304. # In[ ]:
  305.  
  306.  
  307. from elasticsearch import Elasticsearch
  308. es = Elasticsearch()
  309. es.indices.delete(index='resume')
  310.  
  311.  
  312. # In[ ]:
  313.  
  314.  
  315. import requests
  316. from bs4 import BeautifulSoup
  317. r = requests.get('https://www.indeed.com/resumes?q=data+scientist&l=GTA%2C+ON&co=CA&cb=jt&start=')
  318. soup = BeautifulSoup(r.text,'html.parser')
  319. content = soup.find_all('a', class_= 'app_link')
  320.  
  321.  
  322. # In[ ]:
  323.  
  324.  
  325. len(content)
  326.  
  327.  
  328. # In[ ]:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement