Python index function
<p>I am writing a simple Python program which grabs a webpage and finds all the URL links in it. However I try to index the starting and ending delimiter (") of each href link but the ending one always indexed wrong.</p>

<pre><code># open a url and find all the links in it
import urllib2

url=urllib2.urlopen('right.html')
urlinfo = url.info()
urlcontent = url.read()
bodystart = urlcontent.index('&lt;body')
print 'body starts at',bodystart
bodycontent = urlcontent[bodystart:].lower()
print bodycontent

linklist = []
n = bodycontent.index('&lt;a href=')
while n:
    print n
    bodycontent = bodycontent[n:]
    a = bodycontent.index('"')
    b = bodycontent[(a+1):].index('"')
    print a, b
    linklist.append(bodycontent[(a+1):b])
    n = bodycontent[b:].index('&lt;a href=')

print linklist
</code></pre>

<p>I would suggest using a html parsing library instead of manually searching the DOM String.</p>

<p>Beautiful Soup is an excellent library for this purpose. Here is the reference <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/" rel="nofollow">link</a></p>

<p>With bs your link searching functionality could look like:</p>

<pre><code>from bs4 import BeautifulSoup
soup = BeautifulSoup(bodycontent, 'html.parser')
linklist = [a.get('href') for a in soup.find_all('a')]
</code></pre>

python