Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Regex - from Xpath - TypeError: '_sre.SRE_Match' object is not subscriptable

The following code downloads a webpage, finds and element then runs a regular expression to parse a number out of the string. It seems to work on my python 3.7 test system but not my python 3.5. I am downloading a webpage, finding a text block using Xpath. The xpath returns something like 'International (21)' or 'Books (99)' I want to extract out the number, the 21 or 99.

In python 3.5 I get back 'TypeError: '_sre.SRE_Match' object is not subscriptable.'

I am not convinced the error is the differences in versions but that that is the only known diff. The xpath seems to be working as it returns '<_sre.SRE_Match object; span=(14, 18), match='(21)'>' when I print CountObj.

Is there a tweak I should make for python 3.5, is there a better way to code this?

driver = webdriver.Chrome()
driver.get(url); #Download the URL passed from mysql

CatAndCount =  driver.find_element_by_xpath('//h2 [@class="searchResultsTitle"]').text 
 # the above line returns with a name and value like 'International (21)'

CountObj = re.search("\((.*?)\)",CatAndCount)  # look for the number, 21 in example
print (CountObj) # for testing
CountVal=CountObj[1]
like image 521
personalt Avatar asked Jan 15 '18 19:01

personalt


1 Answers

You need to call the group() method on the re.MatchObject with the number of captured group as the parameter to get that (blank or 0 for the whole match). So, to get the first captured group:

CountObj.group(1)

Edit:

If you have multiple captured groups, and want them all, then use groups() method to get them as a tuple e.g.:

CountObj.groups()

Or if you want specific ones e.g. the 1st and 4th captured group, use group() like below to get a tuple of the asked ones:

CountObj.group(1, 4)
like image 112
heemayl Avatar answered Nov 05 '22 18:11

heemayl