Extracting a Link
Extracting a Link#
Applying the same methodology for extracting a hextet, we can extract the url from an html anchor tag. The steps can be enumerated:
use
find()
to identify the index location of the first tag on the page - using as an argument tofind()
the string<a href=
by the same method, find the quote
"
mark’s index positionthen the second quote mark
extract the url from between those two quote marks.
# our data
page = """
<h1>Lorem ipsum dolor sit amet.</h1>
<ul>
<li>
<a href="https://brave.com">Search</a>
</li>
<li><a href="https://docs.python.org/3">Python docs</a></li>
</ul>
"""
# find the first import index value
start_link = page.find("<a href=")
# find first quotation mark
start_quote = page.find('"', start_link)
# find second quotation mark
# to find the final quote enclosing the url we increment the start_quote index value
end_quote = page.find('"', start_quote + 1)
# now we use string slicing for extraction
url = page[start_quote + 1: end_quote]