3

I have a list from a stock web scraper looking like this: [......', 'xlnx>XLNX<', 'yhoo>YHOO<']

how can I get the dictionary with only the quotes? I know this is simple but I could use some help. Thanks

import urllib
import re

base_url = 'http://www.nasdaq.com/markets/indices/nasdaq-100.aspx'
content = urllib.urlopen(base_url).read()
list = re.findall('http://www.nasdaq.com/symbol/(.*)/a>', content)
print list
0

3 Answers 3

1

You have a list, not a dictionary. Also you shouldn't name your variable list as it is the name of a built-in.

>>> content
['xlnx>XLNX<', 'yhoo>YHOO<']
>>> tickers = []
>>> for s in content:
...     tickers.append(''.join(i for i in s if i.isupper()))
... 
>>> tickers
['XLNX', 'YHOO']
Sign up to request clarification or add additional context in comments.

1 Comment

@DSM cause it was 1 AM in the morning :)
1

You should be parsing the HTML with an HTML parser (I always recommend BeautifulSoup), not with regex:

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

Outputs:

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

1 Comment

You mean Beautifulsoup AND a regular expression :-)
1

Something like this:

>>> lis=['xlnx>XLNX<', 'yhoo>YHOO<']
>>> [x[x.index('>')+1:x.index('<')] for x in lis]
['XLNX', 'YHOO']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.