Matching tags in BeautifulSoup
I'm trying to count the number of tags in the 'soup' from a beautifulsoup resu开发者_Python百科lt. I'd like to use a regular expression but am having trouble. The code Ive tried is as follows:
reg_exp_tag = re.compile("<[^>*>")
tags = re.findall(reg_exp_tag, soup(cast as a string))
but re
will not allow reg_exp_tag
, giving an unexpected end of regular expression error.
Any help would be much appreciated!
Thanks
If you've already parsed the HTML with BeautifulSoup, why parse it again? Try this:
num_tags = len(soup.findAll())
Shouldn't that be "<[^>]*>"
instead of "<[^>*>"
?
(the class needs to be closed with a ]
)
精彩评论