Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:38:35+00:00 2026-06-15T18:38:35+00:00

I am using Beautiful Soup to pull out specific div tags, and it seems

  • 0

I am using Beautiful Soup to pull out specific div tags, and it seems I can’t use
simple string matching.

The page has some tags in the form of

<div class="comment form new"...> 

which I want to ignore, and also some tags in the form of

<div class="comment comment-xxxx..."> 

where the x’s represent an integer of arbitrary length, and the ellipses represents an arbitrary number of other values separated by white spaces (that I’m not concerned about). I can’t figure out the
correct regex expression, especially since I’ve never used python’s re class.

Using

soup.find_all(class_="comment") 

finds all tags starting with the word comment. I have tried using

soup.find_all(class_=re.compile(r'(comment)( )(comment)'))
soup.find_all(class_=re.compile(r'comment comment.*'))

and lots of other variations, but I think I’m missing something obvious here about how regex expressions or match() work. Can anyone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:38:37+00:00Added an answer on June 15, 2026 at 6:38 pm

    I think I’ve got it:

    >>> [div['class'] for div in soup.find_all('div')]
    [['comment', 'form', 'new'], ['comment', 'comment-xxxx...']]
    

    Notice that, unlike the equivalent in BS3, it’s not this:

    ['comment form new', 'comment comment-xxxx...']
    

    And that’s why your regexps won’t match.

    But you can match, e.g., this:

    >>> soup.find_all('div', class_=re.compile('comment-'))
    [<div class="comment comment-xxxx..."></div>]
    

    Note that BS does the equivalent of re.search, not re.match, so you don’t need 'comment-.*'. Of course if you want to match 'comment-12345' but not 'comment-of-another-kind you’d want, e.g., 'comment-\d+'.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Beautiful Soup module, how can I get data of a div tag whose
I am data mining a website using Beautiful Soup . The first page is
I'm using Beautiful Soup, but couldn't figure out how to do it. </td> <td
I'm writing a page scraper using beautiful soup, and have noticed it will sometimes
I'd like to do a very simple replacement using Beautiful Soup. Let's say I
I am using Beautiful Soup to try and scrape a page. I am trying
I'm trying to parse a web page using Python's beautiful soup Python parser, and
Im having trouble parsing a HTML page using Beautiful Soup 3, and python 2.6.
I am using python's beautiful stone soup to extract data from this web page
Hi I am using python/beautiful soup to scrap information from foursquare's XML it seems

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.