I run to get some value as score.
score = soup.find('div', attrs={'class' : 'summarycount'})
I run 'print score' to get as follows.
<div class=\"summarycount\">524</div>
I need to extract the number part. I used re module but failed.
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)
It returns an object, which you can use for further searches or to extract its contents with 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
Prints:
<class 'BeautifulSoup.Tag'>
[u'524']
The full documentation with multiple examples is available here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With