-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscraper.py
185 lines (174 loc) · 7.16 KB
/
scraper.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
import csv
import itertools
import json
import logging
import random
import string
import time
from collections import deque
import requests
logging.basicConfig(level=logging.INFO)
url = 'https://www.glassdoor.com/graph'
headers = {
'authority': 'www.glassdoor.com',
'method': 'POST',
'path': '/graph',
'scheme': 'https',
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.9',
'apollographql-client-name': 'company.explorer',
'apollographql-client-version': '3.18.12',
'content-length': '1656',
'content-type': 'application/json',
'cookie': 'asst=1703884286.0; gdId=d9f49a39-3a83-4254-9b05-ee508425e1fb; GSESSIONID=undefined; JSESSIONID=8BD9728688F35A560BE14C52BF9086B9; cass=1; companiesClicked=true; AWSALB=a2RFma+/tBhq9j3ija3daoNCuK1UH8HKQd7nivlpngGq5V31uV9XXlHARG405MIDvWKrW8xoY9GyzGxzJPJfU3+lpqqvOvIKKZ6GiAmJcc3TnSW1DDP0q9+7n8Fc; AWSALBCORS=a2RFma+/tBhq9j3ija3daoNCuK1UH8HKQd7nivlpngGq5V31uV9XXlHARG405MIDvWKrW8xoY9GyzGxzJPJfU3+lpqqvOvIKKZ6GiAmJcc3TnSW1DDP0q9+7n8Fc; gdsid=1703884287447:1703887414513:69AF0D0737F607A97F0296018DAD700E; __cf_bm=7ojqBHf.3Na.L6AoKxk6Rk4PkfZ5tXzSp7Vd0C_nrs0-1703887414-1-AR44LMU/L9o7qFj5QfVhf1Nr3q/83wdIFwM0amtWv+1P/C9zHqJLZBoDYp9Vjwp4cUl1pnUX2WIot7YrLAaz9nQTkXd1pPp6Zl2XCFoq9AVr; _cfuvid=ArL2v6Qdq7.cGjhG5waoyDU8c1BitOizDPildIZEHGA-1703887414645-0-604800000',
'gd-csrf-token': 'W4qI4GJFkb3rDWO0KbLjUw:EF2AITEIXWTdlvXJRskcgzt6C-uTRWH4WurHQRk4o4bpFUMmBcxsLiAoe_qNjrulIqPKZF7-DI9rBQ644Vn2cA:JVJdlO9md9osN-m0WbtgFcqEyi1ScjPDjD2siKH1-_k',
'origin': 'https://www.glassdoor.com',
'referer': 'https://www.glassdoor.com/',
'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
}
# List of search strings
search_strings = deque(list(string.ascii_lowercase) + [''])
random.shuffle(search_strings)
# Set of visited companies
visited = set()
# Variables to store the last used employerName and jobTitle
last_employer_name = None
last_job_title = None
for _ in range(len(search_strings)**2):
employer_name = search_strings[0]
job_title = search_strings[-1]
# Your GraphQL query
# Skip if the combination is the same as the last one
if employer_name == last_employer_name or job_title == last_job_title:
continue
logging.info(f"Searching for employer_name {employer_name}* and job_title {job_title}*")
graphql_query = {
"operationName": "AllResultsCompanySearch",
"variables": {
"context": {"domain": "glassdoor.com"},
"employerName": employer_name,
"jobTitle": job_title,
"locationId": 1,
"locationType": "",
"numPerPage": 10
},
"query": """
query AllResultsCompanySearch(
$jobTitle: String,
$employerName: String,
$locationId: Int,
$locationType: String,
$numPerPage: Int,
$context: Context
) {
employerNameCompaniesData: employerSearch(
employerName: $employerName
location: {locationId: $locationId, locationType: $locationType}
numPerPage: $numPerPage
context: $context
sortOrder: MOSTRELEVANT
) {
...CompanySearchResult
__typename
}
directHitCompany: employerSearch(
filterDirectHit: true
employerName: $employerName
location: {locationId: $locationId, locationType: $locationType}
context: $context
sortOrder: MOSTRELEVANT
) {
...CompanySearchResult
__typename
}
jobTitleCompaniesData: employerSearch(
jobTitle: $jobTitle
location: {locationId: $locationId, locationType: $locationType}
numPerPage: $numPerPage
context: $context
sortOrder: MOSTRELEVANT
) {
...CompanySearchResult
__typename
}
}
fragment CompanySearchResult on UgcSearchV2EmployerResult {
employer {
id
shortName
squareLogoUrl
headquarters
size
sizeCategory
overview {
description
__typename
}
primaryIndustry {
industryId
industryName
__typename
}
links {
overviewUrl
__typename
}
counts {
reviewCount
salaryCount
globalJobCount {
jobCount
__typename
}
__typename
}
__typename
}
employerRatings {
overallRating
__typename
}
__typename
}
"""
}
# Update the last used employerName and jobTitle
last_employer_name = employer_name
last_job_title = job_title
# Rotate the deque
search_strings.rotate()
response = requests.post(url, headers=headers, data=json.dumps(graphql_query))
data = response.json()
# Add the data to company_data.csv
with open('company_data.csv', 'a', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
for result in data['data']['employerNameCompaniesData']:
company = result['employer']
logging.info(f"Adding company: {company['shortName']}")
if company['id'] in visited:
continue
time.sleep(1)
writer.writerow([
company['id'],
company['shortName'],
company['squareLogoUrl'],
company['headquarters'],
company['size'],
company['sizeCategory'],
company['overview']['description'],
# company['primaryIndustry'].get('industryId', "unclassified"),
# company['primaryIndustry'].get('industryName', "unclassified"), # TODO: Fix this
company['links']['overviewUrl'],
company['counts']['reviewCount'],
company['counts']['salaryCount'],
company['counts']['globalJobCount']['jobCount'],
result['employerRatings']['overallRating']
])
visited.add(company['id'])