-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquoraScraper.py
executable file
·180 lines (161 loc) · 6.13 KB
/
quoraScraper.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
from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep, time
from re import compile, match, search
from sys import argv
from random import choice
import os
import json
################################################################################
# General Utilities
################################################################################
def scrollBottom(browser):
print 'Starting to scroll'
oldSource = browser.page_source
while True:
sleep(3)
print 'Executing scroll script'
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
newSource = browser.page_source
if newSource == oldSource:
break
oldSource = newSource
print 'Done scrolling'
################################################################################
# Index Page Main
################################################################################
def getTopicsFromScrapeage():
fr = open('topic_urls.txt', mode='r')
lines = fr.read().split('\n')
topic_urls = []
for line in lines:
x = line.split('\t')
topic_urls.append(x[len(x)-1])
return topic_urls
# TODO: NBD Combine these 2 functions
def downloadIndexPage(browser, topic):
url = topic + '?share=1'
try:
browser.get(url)
except:
return "<html></html>"
scrollBottom(browser)
sleep(3)
html_source = browser.page_source
return html_source
def extractQuestionLinks(html_source, useCached=False):
if useCached:
fr = open('index.html' , mode='r')
html_source = fr.read()
soup = BeautifulSoup(html_source)
links = []
for i in soup.find_all('a', { "class" : "question_link" }):
if len(i) >0 :
link = i['href']
try:
links.append(link)
except UnicodeEncodeError:
pass
return links
def getQuestionText(soup):
try:
a = soup.find('div', { "class" : "question_text_edit" }).getText()
return a
except:
return None
def getTopics(soup):
topics = soup.find_all('div', { "class" : "QuestionTopicListItem TopicListItem topic_pill" })
return ', '.join(topic.getText() for topic in topics)
def getAnswerText(answer):
answer_text = answer.find('div', { "class" : "ExpandedQText ExpandedAnswer" })
result = answer_text.getText()
if result:
return result
################################################################################
# Question Page Main
################################################################################
def question(browser, question_url):
if not match('/', question_url):
print 'Bad question url:', question_url
return
url = 'http://www.quora.com' + question_url + '?share=1'
browser.get(url)
scrollBottom(browser)
sleep(3)
html_source = browser.page_source.encode('utf-8')
soup = BeautifulSoup(html_source)
question_text = getQuestionText(soup)
if question_text == None:
return 0
topics = getTopics(soup)
collapsed_answer_pattern = compile('\d+ Answers? Collapsed')
answers = soup.find_all('div', { "class" : "Answer AnswerBase" }) #class="Answer AnswerBase
i = 1
answer_text = ""
for answer in answers:
result = collapsed_answer_pattern.match(answer.getText())
if result or 'add_answer_wrapper' in answer['class']:
continue # skip collapsed answers and answer text box
answer= getAnswerText(answer)
answer_text = answer_text + answer
try:
dict= {'topics': topics, 'question': question_text, 'answers':answer_text}
except UnicodeDecodeError:
print 'Unicode decode error'
return 0
#append this dict to previous dict in our answers file
a = []
if not os.path.isfile('answers.csv'):
a.append(dict)
with open ('answers.csv', 'w')as f:
f.write(json.dumps(a,indent=2))
else:
with open ('answers.csv') as file:
feeds = json.load(file)
feeds.append(dict)
with open('answers.csv', mode= 'w') as f:
f.write(json.dumps(feeds,indent=2))
################################################################################
# Main
################################################################################
def main(argv):
chromedriver = "/anaconda/bin/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
start = time()
option = argv
if option == 'getquestionlinks':
browser = webdriver.Chrome(chromedriver)
topic_urls = getTopicsFromScrapeage()
for topic_url in topic_urls:
html_source = downloadIndexPage(browser, topic_url)
links = extractQuestionLinks(html_source, False)
with open('questions.txt', mode='a') as file:
file.write('\n'.join(links).encode('utf-8'))
elif option == 'downloadquestions':
browser = webdriver.Chrome(chromedriver)
links = []
done = []
with open('questions.txt', mode='r') as file:
links = file.read().split('\n')
try:
with open('questions-done.txt', mode='r') as file:
done = file.read().split('\n')
except IOError:
done = []
links_set = set(links)
done_set = set(done)
links_not_done_unique = list(links_set.difference(done_set))
print len(links_not_done_unique), 'remaining'
for link in links_not_done_unique:
print link
res = question(browser, link)
if res != 0:
with open('questions-done.txt', mode='a') as file:
try:
file.write((link + '\n').encode('utf-8'))
except (UnicodeDecodeError, UnicodeEncodeError):
print 'An encoding problem occured'
end = time()
print 'Script runtime: ', end - start
if __name__ == "__main__":
main(argv= "getquestionlinks")