Aran Freel
Aran Freel

Reputation: 3215

Python 3.4 : Regex

Question

I am pulling information from YahooFinance. I have a regex statement that finds the necessary information and divides it into 5 different groups. I only need one group to be printed out of the five of them. How can I do this?

        try:

        def isYhooStats():

            headers = {}
            headers['User-Agent'] = "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"

            req = urllib.request.Request(isUrl)
            resp = urllib.request.urlopen(req)
            respData = resp.read()
            dRespDataIs = respData.decode('utf-8')

            netInc= re.search(r'(Net Income)\s*(</strong>)\s*(</td><td align="right">)\s*(<strong>)\s*(\(?\d*,?\d*,\d*\)?)', dRespDataIs)

            print(netInc.groups())

        isYhooStats()

    except IndexError:
        pass

    except AttributeError:
        pass

Outcome

('Net Income', '</strong>', '</td><td align="right">', '<strong>', '4,956,000')

I am printing all of the groups when I only need '4,956,000'

Upvotes: 0

Views: 42

Answers (1)

Kasravnd
Kasravnd

Reputation: 107287

So instead of print(netInc.groups()) you can pass the number of your expected group to group method.

For example in this case you can do :

print(netInc.group(5))

Upvotes: 1

Related Questions