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 3333402
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:47:34+00:00 2026-05-17T23:47:34+00:00

I am using BeautifulSoup in a simple function to extract links that have all

  • 0

I am using BeautifulSoup in a simple function to extract links that have all uppercase text:

def findAllCapsUrls(page_contents):
    """ given HTML, returns a list of URLs that have ALL CAPS text
    """
    soup = BeautifulSoup.BeautifulSoup(page_contents)
    all_urls = node_with_links.findAll(name='a')

    # if the text for the link is ALL CAPS then add the link to good_urls
    good_urls = []
    for url in all_urls:
        text = url.find(text=True)
        if text.upper() == text:
            good_urls.append(url['href'])

    return good_urls

Works well most of the time, but a handful of pages will not parse correctly in BeautifulSoup (or lxml, which I also tried) due to malformed HTML on the page, resulting in an object with no (or only some) links in it. A “handful” might sound like not-a-big-deal, but this function is being used in a crawler so there could be hundreds of pages that the crawler will never find…

How can the above function be refactored to not use a parser like BeautifulSoup? I’ve searched around for how to do this using regex, but all the answers say “use BeautifulSoup.” Alternatively, I started looking at how to “fix” the malformed HTML so that is parses, but I don’t think that is the best route…

What is an alternative solution, using re or something else, that can do the same as the function above?

  • 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-05-17T23:47:34+00:00Added an answer on May 17, 2026 at 11:47 pm

    I ended up with a combination of regex and BeautifulSoup:

    def findAllCapsUrls2(page_contents):
        """ returns a list of URLs that have ALL CAPS text, given
        the HTML from a page. Uses a combo of RE and BeautifulSoup
        to handle malformed pages.
        """
        # get all anchors on page using regex
        p = r'<a\s+href\s*=\s*"([^"]*)"[^>]*>(.*?(?=</a>))</a>'
        re_urls = re.compile(p, re.DOTALL)
        all_a = re_urls.findall(page_contents)
    
        # if the text for the anchor is ALL CAPS then add the link to good_urls
        good_urls = []
        for a in all_a:
            href = a[0]
            a_content = a[1]
            a_soup = BeautifulSoup.BeautifulSoup(a_content)
            text = ''.join([s.strip() for s in a_soup.findAll(text=True) if s])
            if text and text.upper() == text:
                good_urls.append(href)
    
        return good_urls
    

    This is working for my use cases so far, but I wouldn’t guarantee it to work on all pages. Also, I only use this function if the original one fails.

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

Sidebar

Related Questions

No related questions found

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.