开发者

Matching tags in BeautifulSoup

开发者 https://www.devze.com 2022-12-10 09:10 出处:网络
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.

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 ])

0

精彩评论

暂无评论...
验证码 换一张
取 消