-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_scrape.py
79 lines (62 loc) · 1.89 KB
/
web_scrape.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
import requests
from bs4 import BeautifulSoup
# URLS
main_url = 'https://uwaterloo.ca/registrar/important-dates/list?academic_year=229&date=All&page=0'
def next_page():
''' modifies the url to point to the next page '''
global main_url
old_index = int(main_url[-1:])
new_index = old_index + 1
new_url = main_url[:-1] + str(new_index)
main_url = new_url
return formatted(new_url)
def nextp():
next_page()
current_page()
def prev_page():
''' modifies the url to point to the previous page '''
global main_url
old_index = int(main_url[-1:])
if old_index <= 0:
new_index = old_index
else:
new_index = old_index - 1
new_url = main_url[:-1] + str(new_index)
main_url = new_url
return formatted(new_url)
def prevp():
prev_page()
current_page()
def html_parsing(html):
''' given the html for one table row, will parse into [name, date] form '''
html = str(html)
name = html.split('<td>')[1].split('<a class="uw-imp-dates-loader" ')[1].split('>')[1].split('<')[0]
date = html.split('<td>')[-1].split('<')[0]
return [name, date]
def retrieve_site_data(url):
''' retrieve html data from url '''
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
html = soup.select('tbody tr')
return html
def formatted(url):
''' formats url data '''
lst = []
data = retrieve_site_data(url)
for row in data:
lst.append(html_parsing(row))
return lst
def current_page():
''' current page '''
#return (formatted(main_url))
string = ''
for i in formatted(main_url):
string = string + '**' + i[0] + '**' + '\t' + i[1] + '\n'
return string
def reset_page_count():
global main_url
main_url = main_url[:-1] + '0'
def helpme():
''' returns list of all commands '''
s = open('helpme.txt', 'r')
return s.read()