-
Notifications
You must be signed in to change notification settings - Fork 2
/
indeed_api_data.py
148 lines (121 loc) · 4.12 KB
/
indeed_api_data.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
##--------------------------------------------------
##
## Indeed API Job data retrieval script
##
##
##
##--------------------------------------------------
# Python 2.7:
import urllib2
from lxml import etree
import numpy as np
import pandas as pd
import sqlite3
import os
# Python 3.4:
# import urllib
# import urllib.request
wd = os.getcwd() # Edit this for current path
os.chdir(wd)
######
# Define XML Parameters
publisher_id = 'Your Key Here'
v = '2'
format = 'json'
callback = ''
q = 'data+scientist' # QUERY
location = ''
sort = ''
radius = ''
st = 'jobsite'
jt = ''
start = 0
limit = '51' # NOT EMPTY
fromage = '60'
highlight = '0'
filter = '1'
latlong = '1'
co = 'us'
chnl = ''
userip = '1.2.3.4'
useragent = 'Mozilla/%2F4.0%28Firefox%29'
xml_string = 'http://api.indeed.com/ads/apisearch?publisher=' + publisher_id + '&q=' + q +'&l=' + location +\
'&sort=' + sort + '&radius=' + radius + '&st=' + st + '&jt=' + jt + '&start=' + str(start) +\
'&limit=' + limit + '&fromage=' + fromage + '&filter=' + filter + '&latlong=' + latlong +\
'&co=' + co + '&chnl=' + chnl + '&userip=' + userip + '&useragent=' + useragent + '&v=' + v
# Python 2.7:
job_xml = urllib2.urlopen(xml_string).read()
job_tree = etree.HTML(job_xml)
# Python 3.4:
# job_xml = urllib.request.urlopen(xml_string).read()
# job_tree = etree.HTML(job_xml)
[num_results] = job_tree.xpath('//totalresults/text()')
num_results = min(int(num_results), int(limit))
city_list = []
state_list = []
snippet_list = []
lat_list = []
long_list = []
date_list = []
for p in range(0, num_results, 25):
print('Retrieving records from page '+ str(p/25 + 1) +' out of '+ str(len(range(0,num_results,25))) +' pages.')
start = p
xml_string = 'http://api.indeed.com/ads/apisearch?publisher=' + publisher_id + '&q=' + q +'&l=' + location +\
'&sort=' + sort + '&radius=' + radius + '&st=' + st + '&jt=' + jt + '&start=' + str(start) +\
'&limit=' + limit + '&fromage=' + fromage + '&filter=' + filter + '&latlong=' + latlong +\
'&co=' + co + '&chnl=' + chnl + '&userip=' + userip + '&useragent=' + useragent + '&v=' + v
# Python 2.7:
job_xml = urllib2.urlopen(xml_string).read()
# Python 3.4:
# job_xml = urllib.request.urlopen(xml_string).read()
job_tree = etree.HTML(job_xml)
r_index = min(25,num_results - p)
for r in range(r_index):
try:
result_tag = job_tree.xpath('//result')[r]
except:
city_list.append('')
state_list.append('')
snippet_list.append('')
lat_list.append('')
long_list.append('')
city_list.append('')
try:
city_list.append(result_tag.find('city').text)
except:
city_list.append('')
try:
state_list.append(result_tag.find('state').text)
except:
state_list.append('')
try:
snippet_list.append(result_tag.find('snippet').text)
except:
snippet_list.append('')
try:
lat_list.append(result_tag.find('latitude').text)
except:
lat_list.append('')
try:
long_list.append(result_tag.find('longitude').text)
except:
long_list.append('')
try:
date_list.append(result_tag.find('date').text)
except:
date_list.append('')
job_frame = pd.DataFrame({'city': city_list, 'state': state_list, 'snippet': snippet_list,
'latitude': lat_list, 'longitude': long_list, 'date': date_list})
######
# Function to save DataFrame to sqlite-db
def saveFrameToTable(dataFrame, tableName, sqldbName, dbFolder, e_option):
if not os.path.exists(dbFolder):
os.makedirs(dbFolder)
conn = sqlite3.connect(dbFolder + sqldbName + '.db')
print("Database created/opened successfully.")
dataFrame.to_sql(tableName, conn, flavor='sqlite', if_exists=e_option)
conn.close()
data_folder = wd + '\\data\\'
saveFrameToTable(job_frame, 'job_data', 'job_db', data_folder, 'replace')
output_file = data_folder + 'job_data.csv'
job_frame.to_csv(output_file)