user371012 user371012 - 1 year ago 77
HTML Question

how can I get href links from html code

import urllib2

website = "WEBSITE"

openwebsite = urllib2.urlopen(website)

html =

print html

so far so good.

But i want only href links from the plain text html.

How can i solve this problem?

Answer Source

Try with Beautifulsoup:

from BeautifulSoup import BeautifulSoup
import urllib2
import re

html_page = urllib2.urlopen("")
soup = BeautifulSoup(html_page)
for link in soup.findAll('a'):
    print link.get('href')

In case you just want links starting with http://, you should use:

soup.findAll('a', attrs={'href': re.compile("^http://")})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download