Beautiful Soup – Example 1

Beautiful Soup is: a PythonHTML/XML parser designed for quick turnaround projects like screen-scraping. I’m finding it extremely useful as an aid for turning static HTML sites in to dynamic, database driven sites. For example, scraping the desired html data, dumping it into a CSV file, and importing it into MySQL.

In this particular example, we have a bunch of header files, and some are followed by an unordered list full of links. I wanted to:

  1. Identify if a list was preceded by a header.
  2. If we found one, scrape the link, link text, identify if it is a local PDF file or external link, and if it’s an external link, grab the page title (for use in the link’s title text).
  3. If the link returned a 404 Error, make note, but don’t put it into the…
  4. CSV file, which will be created by the Python dictionary created in the previous steps.
  5. I also wrote a function that strips out new line characters – handy.

Here’s the raw HTML that I’m dealing with (excuse the broken images, we don’t need them here anyways).

You can see we have 3 link categories (based on the headers):

  1. magazine articles
  2. web and newsletter writing
  3. promotion tools

This script I wrote creates a dictionary with those values as keys, then creates a list full of tuples for the corresponding links. In the final stages, it outputs all valid data into a CSV file. (Note that the csv file will be created in the same directory that the script was run from.) And don’t forget, if you run it from the command line like so, you’ll get an interactive prompt that will let you experiment with the data that was generated:

python -i

I hope you find it useful! Please leave any comments, suggestions, bugs, and/or improvements below – cheers.

5 thoughts on “Beautiful Soup – Example 1

  1. Hey Wolf! Wish I can answer that for you. The guy that develops Soup is actually very nice and responds to requests – I’m totally unfamiliar with 2.6!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>