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><code>b</code> is relative to the substring from <code>a+1</code>, hence the access to the array should be:</p>

<pre><code>linklist.append(bodycontent[(a+1):(a+1+b)])
</code></pre>

<p>As mentioned in other answers, it's usually preferable to work with a designated library, like <a href="https://pypi.python.org/pypi/beautifulsoup4" rel="nofollow">BeautifulSoup</a>.</p>

python