-
Notifications
You must be signed in to change notification settings - Fork 0
/
quora-question-scraper.py
97 lines (76 loc) · 2.85 KB
/
quora-question-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
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import os
import datetime
import time
import re
from ordered_set import OrderedSet
# start time
start_time = datetime.datetime.now()
today = datetime.date.today()
# dd/mm/YY
date = today.strftime("%d-%m-%Y")
# read topics form a file
file_question_topics = open("topic_list.txt", mode='r', encoding='utf-8')
topics = file_question_topics.readlines()
for topic in topics:
# instantiate a chrome options object so you can set the size and headless preference
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument(" --window-size=1920x1080")
chrome_driver = os.getcwd() +"/chromedriver"
# Set the browser settings to web driver
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
url = "https://nl.quora.com/search?q="+topic+"&type=question"
print("Search questions for: ", url)
driver.get(url)
# define pause time for browser
SCROLL_PAUSE_TIME = 3
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
# infinite while loop, break it when you reach the end of the page or not able to scroll further.
while True:
html_source = " "
i = 0
# try to scroll 5 times in case of slow connection
while i < 5:
# Scroll down to one page length
driver.execute_script("window.scrollBy(0, 1080);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# get page height in pixels
new_height = driver.execute_script("return document.body.scrollHeight")
# break this loop when you are able to scroll further
if new_height != last_height:
break
i += 1
# get html page source
html_source = driver.page_source
# Regex to find questions: https://nl.quora.com/some-question
regex_Qs = r"https:\/\/nl\.quora\.com\/[A-Z][A-Za-zÀ-ž0-9\%\\u0370-\\u03FF\\u0400\-\\u04FF]+[A-Za-zÀ-ž0-9]"
matches = re.findall(regex_Qs, html_source, re.MULTILINE)
unique_questions = OrderedSet()
for match in matches:
clean_match = match.split('?')[0]
unique_questions.add(clean_match)
time.sleep(SCROLL_PAUSE_TIME)
# not able to scroll further, break the infinite loop
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# write content of set to a file called question_urls_date.csv
questions_directory = 'questions'
os.makedirs('questions', exist_ok=True)
file_name = questions_directory + '/' + topic + '_question_urls_'+date+'.csv'
file_question_urls = open(file_name, mode='w', encoding='utf-8')
for question in unique_questions:
link_url = question
print(link_url)
file_question_urls.write(question + "\n")
file_question_urls.close()
print('quitting chrome')
driver.quit()
# finish time
end_time = datetime.datetime.now()
print(end_time-start_time)