-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVapeProject.py
164 lines (130 loc) · 3.99 KB
/
VapeProject.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
from bs4 import BeautifulSoup
import os
import operator
import praw
import time
# Left file paths blank for security reasons
d = ""
o = ""
uAgent = ""
# Sets up reddit comment scraper
bot = praw.Reddit(user_agent = uAgent, client_secret ="", client_id="", usename="");
searchSubs = ['vapeheads', 'ShakeAndVape', 'vaping101', 'juiceswap', 'EJuicePorn']
# Scrapes comments and saves them
for i in searchSubs:
subreddit = bot.subreddit(i)
name = "output"+i+".csv"
output = open(name, "w")
for submission in subreddit.submissions():
submission.comments.replace_more(limit=0)
for comment in submission.comments.list():
print(comment.body)
s = str(comment.body)
s.strip(",")
output.write(s+",")
output.close()
# Just a helper function, saves some line of code
def collect_files(directory):
files = []
f = open(directory)
for line in f:
files.append(line)
return files
def clean_text(s: str):
s = s.lower()
t=""
for i in s:
if i in "qwertyuiopasdfghjklzxcvbnm ":
t += i
return t
class Scraper:
"""scrapes flavor pages collected"""
def __init__(self, directory: str, outputfile: str):
print("Started")
self.directory = directory
self.outputfile = outputfile
self.flavors = []
def scanner(self):
print("scanning")
files = collect_files(self.directory)
for file in files:
try:
soup = BeautifulSoup(open("flavors"+file[1:-1]))
print(soup)
for link in soup.find_all("a"):
if "flavor" in link.get("href"):
print(link)
self.flavors.append(link.text)
print(link.text)
except:
pass
def save(self):
print("Saving")
f = open(self.outputfile, "w")
for i in self.flavors:
print(i)
f.write(i+"\n")
f.close()
def cleaner(inp: str, output: str):
f = open(inp)
o = open(output, "w+")
for line in f:
t = ""
line = line.lower()
for i in line:
if i in "qwertyuiopasdfghjklzxcvbnm ":
t+=i
if t == "" or t == " ":
pass
else:
o.write(t+"\n")
f.close()
o.close()
class Recipe_scraper:
"""Scrapes recipe pages for flavors used -- really should have just made this an extra method"""
def __init__(self, directory: str, outputfile: str):
print("Started")
self.directory = directory
self.outputfile = outputfile
self.flavors = {}
def scanner(self):
files = collect_files(self.directory)
for file in files:
file = file.strip("\n")
f = open(file)
try:
soup = BeautifulSoup(open(file), "lxml")
for link in soup.find_all("a"):
if "flavor" in link.get("href"):
t = link.text
t = clean_text(t)
if t in self.flavors.keys():
self.flavors[t] = self.flavors[t]+1
else:
self.flavors[t] = 1
except:
pass
self.flavors = sorted(self.flavors.items(), key=operator.itemgetter(1))
print(self.flavors)
def save(self):
print("Saving")
f = open(self.outputfile, "w")
for i in self.flavors:
print(i)
f.write(str(i)+"\n")
f.close()
def cleaner(inp: str, output: str):
f = open(inp)
o = open(output, "w+")
for line in f:
t = ""
line = line.lower()
for i in line:
if i in "qwertyuiopasdfghjklzxcvbnm ":
t+=i
if t == "" or t == " ":
pass
else:
o.write(t+"\n")
f.close()
o.close()