-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathycombinator_scraper.py
470 lines (310 loc) · 14 KB
/
ycombinator_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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# -*- coding: utf-8 -*-
"""ycombinator_scraper.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ZEil8gXiThybKkZhrV2AoyiJD9PsmVb3
# Scraping Companies Information for listed companies on Ycombinator
<br>
<br>
<br>
# Project Outline
1. To start with, I'm going to scrape about 1000 entries from https://ycombinator.com/companies, which are:
- The listed company names
- The company's ycombinator page url
- The company location (will prefer to get it here since it is written with the country, unlike how it appeared when the company name has been clicked
- The company description head/slogan. Then,
![Untitled-2.png](attachment:Untitled-2.png)
<br>
<br>
<br>
2. I'll go through the scraped company's ycombinator page url and grab many other informations (company's description, year founded, team size, company page url, social media urls, management details) as they appear on the page.
3. At the end, I will create for each company, a CSV file in the following format:
```
'Company_Name'| 'Company_Page_URL'| 'Company_Location'| 'Description_Head'| 'Website'| 'Description'| 'Founded'| 'Team_Size'| 'Linkedin_Profile'| 'Twitter_Profile'| 'Facebook_Profile'| 'Crunchbase_Profile'| 'Active_Founder1'| 'Active_Founder2'| 'Active_Founder3'
Airbnb| https://www.ycombinator.com/companies/airbnb| San Francisco, CA, US,| Book accommodations around the world.| http://airbnb.com | Founded in August of 2008 and based in San Fra... | 2008 | 5000 | https://www.linkedin.com/company/airbnb/ | https://twitter.com/Airbnb | https://www.facebook.com/airbnb/ | https://www.crunchbase.com/organization/airbnb | Nathan Blecharczyk\nNone\nhttps://twitter.com/... | Brian Chesky\nNone\nhttps://twitter.com/bchesky\n | Joe Gebbia\nNone\nhttps://twitter.com/jgebbia\n,```
## Import necessary libraries
- use **selenium** to downlaod the page
- use **BS4** to parse and extract information
- convert to a Pandas dataframe
lets import the necessary packages
"""
pip install selenium
# import necessary modules
import time
from datetime import datetime as dt
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
import requests
import pandas as pd
import numpy as np
def chrome(mode='h'):
''' A function to instantiate chrome driver
:arguments:
mode - representing either headless (preferred) or browser mode.
:returns:
driver - the driver object instantiated.
'''
if mode == 'h':
# Headless mode
chrome_option = Options()
chrome_option.add_argument("--headless")
chrome_option.add_argument("--log-level=3") # disabling unwanted messages printed while running with am headless browser
driver = webdriver.Chrome(options=chrome_option)
elif mode == 'b':
# Browser mode
driver = webdriver.Chrome()
else:
print("Mode is invalid")
return None
return driver
def get_ycombinator_page_source(page_url=None, browser=None):
"""
A function to get the page source codes and contents
:arguments:
page_url - the url for of the page
browser - the webdriver object
:returns:
page_dom - a beautiful soup object of the page contents.
"""
time.sleep(5)
browser.get(page_url)
time.sleep(20) # allow the page to load
################ implement infinite scrolling ######################################
try:
previous_height = browser.execute_script('return document.body.scrollHeight')
i = 0
while True:
i+=1
print("scroll: ", i)
browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')
time.sleep(1)
new_height = browser.execute_script('return document.body.scrollHeight')
if previous_height == new_height:
print("End of page reached")
break
previous_height = new_height
except:
browser.close()
################################################################################
time.sleep(2)
page_dom = BeautifulSoup(browser.page_source,"html.parser")
return page_dom
def get_company_page_info(doc):
"""
A function to get the names of startup companies, location and url of company pages on ycombinator
:arguments:
doc - the bs4 object of the ycombinator page
:returns:
DataFrame object of the data collected
"""
page_dict = { 'Company_Name': [], 'Company_Page_URL': [], 'Company_Location': [], 'Description_Head': []}
# find all a tags with class name "styles-module__company___1UVnl no-hovercard" which points to individual company segment
item = doc.find_all("a",{"class":"styles-module__company___1UVnl no-hovercard"})
# company ycompany's page url
url_list = []
# get page informations
for i in range(len(item)):
# company name
company_name = item[i].find('span', {"class":"styles-module__coName___3zz21"})
page_dict["Company_Name"].append(company_name.text)
# company page url
company_page_url = item[i]['href']
page_dict["Company_Page_URL"].append("https://www.ycombinator.com"+company_page_url)
# company ycompany's page url
url_list.append("https://www.ycombinator.com"+company_page_url)
# company location
company_location = item[i].find('span', {"class":"styles-module__coLocation___yhKam"})
page_dict["Company_Location"].append(company_location.text)
# Description_Head
description_head = item[i].find('span', {"class":"styles-module__coDescription___1b_yd"})
page_dict["Description_Head"].append(description_head.text)
return pd.DataFrame(page_dict), url_list
"""
## Now, I will write functions to :
1. Browse each Company_Page_URL
2. Get actual company website address
3. Get the company description as appeared on ycombinator
4. Get the company year founded and team size,
5. Get the company social media urls
6. Get the company founder infos such as there name, position and social media urls
7. Then after collecting the above info for all Company_Page_URL, create a CSV from concatenating the earlier scraped df with the dataframe of the newly collected infos
"""
def get_company_website(doc):
# scrape the websites
try:
company_websites_tags = doc.find("a",{"target":"_blank"})
try:
company_websites = company_websites_tags.text
except:
company_websites = company_websites_tags
except:
company_websites = np.nan
return company_websites
def get_company_description(doc):
# scrape the descriptions
try:
company_description_tags = doc.find("p",{"class":"whitespace-pre-line"})
try:
company_description = company_description_tags.text
except:
company_description = company_description_tags
except:
company_description = np.nan
return company_description
def get_company_year_founded_and_team_size(doc):
# scrape
try:
ppty = doc.find("div",{"class":"space-y-0.5"}).find_all("div",{"class":"flex flex-row justify-between"})
year_founded = ppty[0].text.split(":")[1]
team_size = ppty[1].text.split(":")[1]
company_year = year_founded
company_size = team_size
except:
company_year = np.nan
company_size = np.nan
return company_year, company_size
def get_company_social_media_urls(doc):
# scrape the social network urls
try:
sm_info = doc.find("div",{"class":"space-x-2"})
try:
linkedin_url = sm_info.find("a",{"title":"LinkedIn profile"})["href"]
except:
linkedin_url = np.NaN
try:
twitter_url = sm_info.find("a",{"title":"Twitter account"})["href"]
except:
twitter_url = np.NaN
try:
facebook_url = sm_info.find("a",{"title":"Facebook profile"})["href"]
except:
facebook_url = np.NaN
try:
crunchbase_url = sm_info.find("a",{"title":"Crunchbase profile"})["href"]
except:
crunchbase_url = np.NaN
except:
linkedin_url = np.NaN
twitter_url = np.NaN
facebook_url = np.NaN
crunchbase_url = np.NaN
return linkedin_url, twitter_url, facebook_url, crunchbase_url
def get_founder_info(doc):
founder_dict = {}
try:
founder_info = doc.find_all("div",{"class":"leading-snug"})
except:
founder_info = ""
if len(founder_info)>0:
for i in range(len(founder_info)):
founder_name = founder_info[i].find("div",{"class":"font-bold"}).text
try:
founder_post = founder_info[i].find("div",{"class":""}).text
except:
founder_post = founder_info[i].find("div",{"class":""})
try:
founder_sm = founder_info[i].find("div",{"class":"mt-1 space-x-2"}).find_all("a")
founder_sm_links = ""
for j in founder_sm:
founder_sm_links = founder_sm_links + str(j["href"]) + "\n"
except:
founder_sm_links = "None"
founder = founder_name + "\n" + str(founder_post) + "\n" + founder_sm_links # + "\n" + str(founder_descr)
founder_dict["Active_Founder"+str(i+1)] = founder
return founder_dict
else:
return founder_dict
def scrape_all(url=None):
response = requests.get(url)
# if response.status_code != 200:
# raise Exception('Failed to load page {}'.format(url))
soup_other = BeautifulSoup(response.text,"html.parser")
c_web = get_company_website(soup_other)
c_description = get_company_description(soup_other)
company_year, company_size = get_company_year_founded_and_team_size(soup_other)
linkedin_url, twitter_url, facebook_url, crunchbase_url = get_company_social_media_urls(soup_other)
founder_details = get_founder_info(soup_other)
d = {'Website': c_web,
'Description': c_description,
'Founded': company_year,
'Team_Size': company_size,
'Linkedin_Profile': linkedin_url,
'Twitter_Profile': twitter_url,
'Facebook_Profile': facebook_url,
'Crunchbase_Profile': crunchbase_url,
}
d.update(founder_details)
return d
def make_dataframe_and_save(df1 = None, l=None):
final_df = pd.concat([df,pd.DataFrame(l)], axis=1)
final_df.to_csv("ycombinator_data.csv", index = False)
return final_df
"""# Scrape the list of companies link, name, location and short description(head) from ycomobinator
`chrome`: used to instantiate the webdriver as either a headless browser or not
`get_ycombinator_page_source`: Used to handle the dynamic scraping of the project. It scrolls the website till it reaches the end of the page. Afterward, beautifulsoup is used to parse the page source which is used as input to:
`get_company_page_info`: extracted the COmpany name, links, short description/description head and location of the companies. returns url_list and a dataframe for the already scraped information.
**Note:** _They in total take `~2 minutes` to run_ and the lenght of url_list must be 1000
"""
root_url = " https://ycombinator.com/companies"
driver = chrome("h") # instantiate the webdriver
time_start = time.time()
doc = get_ycombinator_page_source(page_url=root_url, browser = driver)
df, url_list = get_company_page_info(doc)
time_used = time.time() - time_start
print("The time used is %s seconds"%(time_used))
url_list_len = len(url_list)
print("The length of url_list is %s "%(url_list_len))
# close browser
driver.close()
"""# B1. Scraping Companies' information without multi-threading"""
l = []
retries = []
count = 1
# start_no_thread = dt.now()
time_start = time.time()
run = True
while run:
for link in url_list:
try:
print(f"{count}/{url_list_len}", end='\r')
d = scrape_all(url=link)
l.append(d)
except Exception as e:
print(e)
retries.append(link)
if retries != []:
url_list = retries
retries = []
else:
run = False
# runtime_no_thread = (dt.now() - start_no_thread).total_seconds()
# print(f'Total runtime - {runtime_no_thread}')
time_used = time.time() - time_start
print("The time used is %s seconds"%(time_used))
"""# B2. Scraping Companies' information WITH multi-threading"""
import concurrent.futures as cf
start_thread = dt.now()
l1 = []
with cf.ThreadPoolExecutor() as exc:
results = exc.map(scrape_all, url_list)
for result in results:
l1.append(result)
runtime_thread = (dt.now() - start_thread).total_seconds()
print(f'Total runtime - {runtime_thread}')
"""# View and safe the company information DataFrame"""
df_final_threaded = make_dataframe_and_save(df1 = df, l=l1)
print(df_final_threaded.isna().sum())
df_final = make_dataframe_and_save(df1 = df, l=l)
print(df_final.isna().sum())
"""# References and Future Work
### Summary of what I did / Issues
- I have just succesfully done all the outlined procedures from the begiining of the project. However, I made a terrible mistake from making my code into modules/functions. I wasn't considerate of network failure which resulted into the scraper not returning any dataframe in the case of a network failure.
- I hope with guidance and little more time, will be able to correct this error
### References
- How to make infinite scrolling with selenium [https://www.youtube.com/watch?v=qhJ_gMB772U]
### Ideas for future work
- Make the code robust to network failure
"""