原英文标题
How can I get href links from HTML using Python?
import urllib2
website = "WEBSITE"
openwebsite = urllib2.urlopen(website)
html = getwebsite.read()
print html
到现在为止还挺好。
但我只希望纯文本HTML中的href链接。 我怎么解决这个问题?
9 个回复:
===============>>#1 票数:89 已采纳
from BeautifulSoup import BeautifulSoup
import urllib2
import re
html_page = urllib2.urlopen("http://www.yourwebsite.com")
soup = BeautifulSoup(html_page)
for link in soup.findAll('a'):
print link.get('href')
如果您只想要以http://开头的链接,您应该使用:soup.findAll('a', attrs={'href': re.compile("^http://")})
===============>>