-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_scraper_git.py
189 lines (143 loc) · 6.25 KB
/
all_scraper_git.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from selenium_webTest1 import SeleniumWeb
import configparser
import selenium
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import json
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import xml.etree.ElementTree as xml
import xml.etree.ElementTree as xml1
from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
import pandas as pd
#Intializing the webdriver for selenium
options = Options()
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('disable-infobars')
options.add_argument("--disable-extensions")
options.add_argument("--start-maximized")
driver = webdriver.Chrome(chrome_options=options, executable_path='/usr/bin/chromedriver')
import pandas as pd
def createXML_Separate(filename):
filePATH = os.listdir("config/")
prefix = "config/"
filelist = [prefix + filename + ".csv"]
root = xml.Element('items')
tree = xml.ElementTree(root)
for csv in filelist:
df = pd.read_csv(csv)
n = df.shape[0]
for i in range(n):
jobXml = Element('item')
root.append(jobXml)
jobTitle = xml.SubElement(jobXml, 'title')
jobTitle.text = df.iloc[i]['title']
description = xml.SubElement(jobXml, 'description')
description.text = df.iloc[i]['description']
url = xml.SubElement(jobXml, 'url')
url.text = df.iloc[i]['url']
img_job = xml.SubElement(jobXml, 'logo')
img_job.text = df.iloc[i]['logo']
company = xml.SubElement(jobXml, 'company')
company.text = df.iloc[i]['company']
separate_xml = filename + ".xml"
file_obj = open(separate_xml, "wb+")
file_obj.write(str(xml1.tostring(root))[2:-1].encode('utf-8'))
print("Done!")
def createXML(mergedlist):
filelist = mergedlist
print(filelist, "Files to be merged")
root = xml.Element('items')
tree = xml.ElementTree(root)
for csv in filelist:
df = pd.read_csv(csv)
n = df.shape[0]
for i in range(n):
jobXml = Element('item')
root.append(jobXml)
jobTitle = xml.SubElement(jobXml, 'title')
jobTitle.text = df.iloc[i]['title']
description = xml.SubElement(jobXml, 'description')
description.text = df.iloc[i]['description']
url = xml.SubElement(jobXml, 'url')
url.text = df.iloc[i]['url']
img_job = xml.SubElement(jobXml, 'logo')
img_job.text = df.iloc[i]['logo']
company = xml.SubElement(jobXml, 'company')
company.text = df.iloc[i]['company']
file_obj = open("merged.xml", "wb+")
file_obj.write(str(xml1.tostring(root))[2:-1].encode('utf-8'))
print("Done!")
filelist = os.listdir("config/src/src")
print("companies to be scraped :: ", filelist)
separate_list = []
merged_list = []
company_log = []
status_company = []
for file in filelist:
try:
config = configparser.ConfigParser()
file = "config/src/src/" + file
config.read(file)
configfile = config['DEFAULT']
if config.has_option("DEFAULT", 'url') :
url = configfile['url']
else:
print("No URL Specified")
continue
driver.get(url)
print("Let the website load for 10 seconds")
time.sleep(12)
selenium_obj = SeleniumWeb(driver)
if config.has_option("DEFAULT", 'UTM_Source Code') :
utm = configfile['UTM_Source Code']
else:
utm = ""
if config.has_option("DEFAULT", 'buttons') :
buttons = configfile['buttons'].split("|")
selenium_obj.plugin_buttons(buttons)
if config.has_option("DEFAULT", 'keyword'):
if config.has_option("DEFAULT", 'search_bar'):
keyword = configfile['keyword']
searchbar = configfile['search_bar']
selenium_obj.search_keyword(keyword, searchbar)
else:
print("Searching field bar is not specified!! Please check your config file once :)")
exit()
if 'workday' in url:
if config.has_option("DEFAULT", 'right_click') and config.has_option("DEFAULT", 'links') and config.has_option("DEFAULT", 'url_selector'):
links = configfile['links']
right_click = configfile['right_click']
url_selector = configfile['url_selector']
job_links = selenium_obj.retrieve_links_by_right_click(links, right_click, url_selector)
selenium_obj.get_job_data(job_links, configfile['title'], configfile['description'], configfile, utm)
else:
print("Can't fetch Job URLs, please give instructions for right click to select the URL for the particular posting")
else:
links = configfile['links']
link_url = selenium_obj.retrieve_links_directly(links)
selenium_obj.get_job_data(link_url, configfile['title'], configfile['description'], configfile, utm)
filename = "config/" + configfile['company'] + ".csv"
selenium_obj.df.to_csv(filename)
if configfile['separate_url'] == "True":
createXML_Separate(configfile['company'])
else:
merged_list.append(filename)
createXML(merged_list)
print("Job parsing for ",configfile['company'], " done successfully!!")
company_log.append(configfile['company'])
status_company.append("Successful")
except:
print("Cant Scrape for ", configfile['company'], ", ....... skipping !!!")
company_log.append(configfile['company'])
status_company.append("Fail")
stats = {'Company' : company_log, 'Status' : status_company}
df_status = pd.DataFrame(data = stats)
df_status.to_csv("Status_log.csv")