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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:59:31+00:00 2026-05-26T21:59:31+00:00

Given the flexibility of HTML codes, parsing out paragraphs as seen by a user

  • 0

Given the flexibility of HTML codes, parsing out paragraphs as seen by a user through browser seems to be a quite non-trivial task.

For now, I have a not so robust solution:

tree = lxml.etree.fromstring(html, lxml.etree.HTMLParser()) if isinstance(html, basestring) else html 
for skiptag in ('//script', '//iframe', '//style', 
                '//link', '//meta', '//noscript', '//option'):    
    for node in tree.xpath(skiptag):
        node.getparent().remove(node)
paragraphs = lxml.etree.tostring(tree, encoding=unicode, method='text')

The problems I am facing are mainly about how to tackle abnormality (or say, free-styles).

One quite common case is that many paragraphs are written in one line (e.g. code below) in the HTML, and my code will parse them into one paragraph.

<p>bla, bla 1.</p><p><u><span class="colored"><strong>bla, bla 2.</strong></span></u></p><p>bla, bla. 3;</p><p>bla, bla. 3</p> 

My questions are:

  • is there any good ways in general to parse out paragraphs correctly?
  • in this particular case, how should I optimize my code to correctly get paragraphs from one-line of HTML, given not only <p> can represent paragraph, but many other ways may be applied to the free-style?
  • any general advice?
  • 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-26T21:59:31+00:00Added an answer on May 26, 2026 at 9:59 pm

    Have a look at html2text.

    It may not do exactly what you want, but it’s only a 500 line script, so it should be pretty easy to adapt it to your particular needs.

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

Sidebar

Related Questions

Given this markup: // Calendar.html?date=1/2/2003 <script> $(function() { $('.inlinedatepicker').datepicker(); }); </script> ... <div class=inlinedatepicker
I'm creating user customizable HTML templates and deciding on what elements types to use.
My main concern is not so much flexibility ( of course, that's given like
I'm wondering if using generic types through out the program will have any (significant)degrading
Given a package, how can I automatically find all its sub-packages?
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given a date range how to calculate the number of weekends partially or wholly
Given an interface or interfaces, what is the best way to generate an class
Given: unsigned int a, b, c, d; I want: d = a * b
Given Java's write once, run anywhere paradigm and the fact that the Java tutorials

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.