-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyScholarship.py
126 lines (111 loc) · 4.11 KB
/
MyScholarship.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
from ast import Not
from contextlib import nullcontext
from bs4 import BeautifulSoup
import openpyxl
from openpyxl import load_workbook
import requests
class MyScholarships:
MyScholarshipsLink = "https://dallascollege.academicworks.com/opportunities"
def get_soup(link=""):
req = requests.get(link)
soup = BeautifulSoup(req.text, "html.parser")
return soup
@staticmethod
def _get_pages():
pages = []
soup = MyScholarships.get_soup(MyScholarships.MyScholarshipsLink)
for p in soup.find_all('option',attrs={'name':'page'}):
pages.append(p['data-direct-url'])
return pages
@staticmethod
def _get_name(tag):
try:
return tag.find("a").text
except:
print("<a> link tag not present")
return 0
@staticmethod
def _get_link(tag):
try:
return MyScholarships.MyScholarshipsLink + tag.find("a")["href"].replace('opportunities/','')
except:
print("<a> link tag not present")
return 0
@staticmethod
def _get_award(tag):
try:
for td in tag.findAll('td')[:1]:
return td.text.strip()
except:
print("<td> tag with award not found")
return 0
@staticmethod
def _get_deadline(tag):
try:
for td in tag.findAll('td')[1:]:
return td.text.strip()
except:
print("<td> tag with award not found")
return 0
@staticmethod
def _get_questions(lnk):
q = []
try:
soup = MyScholarships.get_soup(lnk)
except:
print(f'Invalid Link')
else:
for l in soup.find_all(class_="js-question"):
q.append(l.text)
return q
@staticmethod
def _get_scholarship():
MyScholarshipsList = []
for page in MyScholarships._get_pages():
soup = MyScholarships.get_soup(MyScholarships.MyScholarshipsLink + page)
for tr in soup.findAll("tr")[1:]:
dic = {
"Deadline" : MyScholarships._get_deadline(tr),
"Award" : MyScholarships._get_award(tr),
"Name" : MyScholarships._get_name(tr),
"Link" : MyScholarships._get_link(tr),
"Questions": MyScholarships._get_questions(MyScholarships._get_link(tr))
}
if dic["Deadline"] == "Ended" or dic["Deadline"] == "":
continue
else:
MyScholarshipsList.append(dic)
return MyScholarshipsList
def scholarship_xlsx(self,filename = "Scholarships.xlsx"):
wb = openpyxl.Workbook()
sheet = wb.active
_row = 1
for dic in MyScholarships._get_scholarship():
sheet.cell(row=_row , column=1).value = dic["Deadline"]
sheet.cell(row=_row , column=2).value = dic["Award"]
sheet.cell(row=_row , column=3).value = dic["Name"]
sheet.cell(row=_row , column=4).value = dic["Link"]
sheet.cell(row=_row ,column=5).value = str(dic["Questions"])
if len(dic["Questions"]) != 0:
for q in dic["Questions"]:
_row += 1
sheet.cell(row=_row , column=1).value = dic["Deadline"]
sheet.cell(row=_row , column=2).value = dic["Award"]
sheet.cell(row=_row , column=3).value = dic["Name"]
sheet.cell(row=_row , column=4).value = dic["Link"]
sheet.cell(row=_row , column=5).value = q
_row += 1
elif len(dic["Questions"]) == 0:
sheet.cell(row = _row, column=5).value = "No Questions"
_row += 1
wb.save(filename)
return filename
def get_list_length(self,filename):
wb = load_workbook(filename)
sheet = wb.active
i = 1
while sheet.cell(row=i,column=1).value != None:
i += 1
return i-1
s = MyScholarships()
print(s.scholarship_xlsx() + "file was made")