Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++CodeDelphi

Quickly Parse HTML And XML With BeautifulSoup Python Library In Delphi And C++ Windows Apps

We know how to load and display Web content or local files in Delphi using TWebBrowser. It offers support for the basic functions of a browser, such as navigate to URL, go back, go forward, along with specific events. How about the web scrapping in Delphi using the Python BeautifulSoup library? Sounds Interesting? Yes, with the help of Python4Delphi we can scrap the web pages quickly in the Delphi/C++ Builder app. This post helps to understand with sample python script.

Delphi itself has extensive XML and HTML parsing capabilities through TXmlDocument. And here is some sample code for utilizing TXmlDocument in Delphi. If you have an existing Python application though you could make use of the BeautifulSoup Python Library to parse XML and HTML in your Python code. If you need extra speed you could bring the XML or HTML data over to Delphi for faster parsing through Python4Delphi. You can use Python4Delphi a number of different ways such as:

  • Create a Windows Python GUI around you existing Python app.
  • Add Python scripting to your Delphi Windows apps.
  • Add parallel processing to your Python apps through Delphi threads.
  • Enhance your speed sensitive Python apps with functions from Delphi for more speed.

Prerequisites.

  • If not python and Python4Delphi is not installed on your machine, Check this, how to run a simple python script in Delphi application using Python4Delphi sample app
  • Open windows open command prompt, and type pip install -U bs4 to install BeautifulSoup4. For more info for Installing Python Modules check here
  • First, run the Demo1 project for executing Python script in Python for Delphi. Then load the script in the Memo1 field and press the Execute Script button to see the result. Go to GitHub to download the Demo1 source.

Beautiful Soup Python Library sample script details: Beautiful Soup works with your favorite parser to provide idiomatic ways of navigating, searching, and modifying the parse tree. It commonly saves programmers hours or days of work. The sample script demonstrates,

  • How to transforms a complex HTML document into a complex tree of Python objects( four kinds of objects: TagNavigableStringBeautifulSoup, and Comment.)
  • How to Navigate the within the tree of Python Objects like Going down, Up, Sideways, Back and Forth, Navigable using Tagnames.
  • Searching the parse tree Objects using two most popular methods: find() and find_all().
  • How to modify the tree and write your changes as a new HTML or XML document.
beautifulsoupdemo 4938218
<strong>BeautifulSoup Python Library Demo<strong>
  • CSS selector against a parsed document and return all the matching elements. Tag has a similar method which runs a CSS selector against the contents of a single tag. check here for more details.
  • You can do much more with this library like Output the Beautiful Soup parse tree into a nicely formatted Unicode string, with a separate line for each tag and each string, Comparing objects for equality, Copying Beautiful Soup objects etc.

Note: Samples used for demonstration were picked from here with only the difference of printing the outputs. You can check the APIs and some more samples from the same place.

You have read the quick overview of Beautiful Soup library, download this library from here and pull data out of html, xml easily in your applications. Check out Python4Delphi and easily build Python GUIs for Windows using Delphi.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES