-
Notifications
You must be signed in to change notification settings - Fork 0
/
L5Q29_NoLinks.py
39 lines (31 loc) · 1.19 KB
/
L5Q29_NoLinks.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
# Modify the get_next_target procedure so that
# if there is a link it behaves as before, but
# if there is no link tag in the input string,
# it returns None, 0.
# Note that None is not a string and so should
# not be enclosed in quotes.
# Also note that your answer will appear in
# parentheses if you print it.
page = '''<div id="top_bin"> <div id="top_content" class="width960">
<div class="udacity float-left"> <a href="testymctesterson">'''
def get_next_target(page):
start_link = page.find('<a href=')
if start_link != -1:
start_quote = page.find('"', start_link)
end_quote = page.find('"', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote
else:
return (None, 0)
#Udacity solution
def get_next_targetUdacity(page):
start_link = page.find('<a href=')
# if the link tag sequence is not found, find returns a -1
if start_link == -1:
# return the error codes of None, 0 now and skip the rest!
return None, 0
start_quote = page.find('"', start_link)
end_quote = page.find('"', start_quote + 1)
url = page[start_quote + 1:end_quote]
return url, end_quote
print(get_next_target(page))