您应该使用 HTML 解析器(我总是推荐BeautifulSoup)来解析 HTML,而不是使用正则表达式:
import re, urllib2
from BeautifulSoup import BeautifulSoup
url = 'http://www.nasdaq.com/markets/indices/nasdaq-100.aspx'
soup = BeautifulSoup(urllib2.urlopen(url))
for link in soup.findAll('a', href=re.compile('/symbol/'))[1:]:
print link.text
输出:
ATVI
ADBE
AKAM
ALXN
ALTR
AMZN
AMGN
APOL
AAPL
AMAT
ADSK
ADP
AVGO
BIDU
BBBY
BIIB
BMC
BRCM
CHRW
CA
CELG
CERN
CHKP
CSCO
CTXS
CTSH
CMCSA
COST
DELL
XRAY
DTV
DLTR
EBAY
ERTS
EXPE
EXPD
ESRX
FFIV
FAST
FISV
FLEX
FOSL
GRMN
GILD
GOOG
GMCR
HSIC
INFY
INTC
INTU
ISRG
KLAC
KFT
LRCX
LINTA
LIFE
LLTC
MRVL
MAT
MXIM
MCHP
MU
MSFT
MNST
MYL
NTAP
NFLX
NUAN
NVDA
NWSA
ORLY
ORCL
PCAR
PAYX
PCLN
PRGO
QCOM
RIMM
ROST
SNDK
STX
SHLD
SIAL
SIRI
SPLS
SBUX
SRCL
SYMC
TXN
VRSN
VRTX
VIAB
VMED
VOD
WCRX
WFM
WYNN
XLNX
YHOO