forked from johannagwan/nus-botfessions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nuswhispers_advice.py
52 lines (41 loc) · 1.33 KB
/
nuswhispers_advice.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
from selenium.webdriver import Chrome
import pandas as pd
import time
webdriver = "D:\Hanna programme downloads\chromedriver_win32\chromedriver.exe"
driver = Chrome(webdriver)
def scroll(driver, timeout):
scroll_pause_time = timeout
i = 0
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(scroll_pause_time)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if i == 2500:
break
# last_height = new_height
i += 1
url = "https://nuswhispers.com/category/10"
driver.get(url)
scroll(driver, 0.1)
total = []
categoriesArr = []
categories = driver.find_elements_by_class_name("post-header")
c = 0
for category in categories:
cat = ((category.text))
categoriesArr.append(cat)
c += 1
contents = driver.find_elements_by_class_name("post-content")
d = 0
for content in contents:
new = ((content.text[9:-16], categoriesArr[d]))
d += 1
total.append(new)
df = pd.DataFrame(total,columns=['confessions', 'categories'])
df.to_csv('confessionsAdvice.csv')
driver.close()