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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:42:05+00:00 2026-05-11T18:42:05+00:00

I have some html files that I want to convert to text. I have

  • 0

I have some html files that I want to convert to text. I have played around with BeautifulSoup and made some progress on understanding how to use the instructions and can submit html and get back text.

However, my files have a lot of text that is formatted using table structures. For example I might have a paragraph of text that resides in a td tag within set of table tags

<table>
<td> here is some really useful information and there might be other markup tags but
     this information is really textual in my eyes-I want to preserve it
 </td>
</table>

And then there are the ‘classic tables’ that have data within the body of the table.

I want to be able to apply an algorithm to the table and set some rules that determine whether the table is ripped out before I convert the document to text.

I have figured out how to get the characteristics of my tables- for example to get the number of cols in each table:

numbCols=[]
for table in soup.findAll('table'):
    rows=[]
    for row in table.findAll('tr'):
        columns=0
        for column in row.findAll('td'):
            columns+=1
        rows.append(columns)
    numbCols.append(rows)

so I can operate on numbCols and use the len of each item in the list and the values in each item in the list to analyze the characteristics of my tables and identify the ones I want to keep or discard.

I am not seeing an elegant way to the use this information with BeautifulSoup to get the text. I guess what I am trying to get at is suppose I analyze numbCols and decide that of the ten tables in a particular document I want to exclude tables 2, 4, 6, & 9. So the part of the html document includes everything but those tables. How can I segment my soup that way?

The solution I have come up with is first identify the position of each of the open and close table tags using finditer and getting the spans and then zipping the spans with the numbCols. I can then use this list to snip and join the pieces of my string together. Once this is finished I can then use BeautifulSoup to convert the html to text.

I feel sure that I should be able to do all of this in BeautifulSoup. Any suggestions or links to existing examples would be great. I should mention that my source files can be large and I have thousands to handle.

Didn’t have the answer but I am getting closer

  • 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-11T18:42:05+00:00Added an answer on May 11, 2026 at 6:42 pm

    Man I love this stuff
    Assuming in a naive case that I want to delete all of the tables that have any rows with a column length greater than 3 My answer is

    for table in soup.findAll('table'):
        rows=[]
        for row in table.findAll('tr'):
            columns=0
            for column in row.findAll('td'):
                columns+=1
                rows.append(columns)
            if max(rows)>3:
              table.delete()
    

    You can do any processing you want at any level in that loop, it is only necessary to identify the test and get the right instance to test.

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

Sidebar

Related Questions

I have some html files with their own css. I want to use them
I have some static resources (images and HTML files) that will be localized. One
I have a .NET console application that needs to generate some HTML files. I
I have some html files and want to extract only lines with containing these
I use DOMDocument for editing some HTML files, but some of theme have in
I have some HTML files that I need to distribute in MS Word doc
I have an XML file containing some scientific text that I want to display
I have some HTML files, and each one of them I want to partially
I have an IntentService that is parsing some xlm file to create a html
I have such files to parse (from scrapping) with Python: some HTML and JS

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.