What is the return value of BeautifulSoup.find?

I run to get some value as an indicator.

score = soup.find('div', attrs={'class' : 'summarycount'}) 

I run 'print score' to get the following.

 <div class=\"summarycount\">524</div> 

I need to extract the number. I used re module but could not.

 m = re.search("[^\d]+(\d+)", score) 
  TypeError: expected string or buffer

 function search in re.py at line 142
 return _compile (pattern, flags) .search (string)
  • What is the return type of the search function?
  • How to get a number from an evaluation variable?
  • Is there any easy way to let BeautifulSoup return a value (in this case 524)?
+4
source share
1 answer

It returns an object that you can use to further search or to extract its contents using score.contents :

 from BeautifulSoup import BeautifulSoup str = r''' <body> <div class="summarycount">524</div> <div class="foo">111</div> </body> ''' soup = BeautifulSoup(str) score = soup.find('div', attrs={'class' : 'summarycount'}) print type(score) print score.contents 

Print

 <class 'BeautifulSoup.Tag'> [u'524'] 

Full documentation with a few examples is available here .

+10
source

Source: https://habr.com/ru/post/1307064/