tags:

views:

34

answers:

2

I have a folder full of Windows .URL files. I'd like to translate them into a list of MLA citations for my paper.

Is this a good application of Python? How can I get the page titles? I'm on Windows XP with Python 3.1.1.

+2  A: 

Given a file that contains an HTML page, you can parse it to extract its title, and BeautifulSoup is the recommended third-party library for the job. Get the BeautifulSoup version compatible with Python 3.1 here, install it, then:

  • parse each file's contents into a soup object e.g. with:

    from BeautifulSoup import BeautifulSoup html = open('thefile.html', 'r').read() soup = BeautifulSoup(html)

  • get the title tag, if any, and print its string contents (if any):

    title = soup.find('title') if title is None: print('No title!') else: print('Title: ' + title.string)

Alex Martelli
+3  A: 

This is a fantastic use for Python! The .URL file format has a syntax like this:

[InternetShortcut]
URL=http://www.example.com/
OtherStuff=irrelevant

To parse your .URL files, start with ConfigParser, which will read this and make an InternetShortcut section that you can read the URL from. Once you have a list of URLs, you can then use urllib or urllib2 to load the URL, and use a dumb regex to get the page title (or BeautifulSoup as Alex suggests).

Once you have that, you have a list of URLs and page titles...not enough for a full MLA citation, but should be enough to get you started, no?

Something like this (very rough, coding in the SO window):

from glob import glob
from urllib2 import urlopen
from ConfigParser import ConfigParser
from re import search

# I use RE here, you might consider BeautifulSoup because RE can be stupid
TITLE = r"<title>([^<]+)</title>"

result = []
for file in glob("*.url"):
    config = ConfigParser.ConfigParser()
    config.read(file)
    url = config.get("InternetShortcut", "URL")

    # Get the title
    page = urlopen(url).read()
    try: title = search(TITLE, page).groups()[0]
    except: title = "Couldn't find title"

    result.append((url, title))

for url, title in result:
    print "'%s' <%s>" % (title, url)
Jed Smith
Thank you both! This is why I love this site. Everyone's so prompt and helpful. I''ll use these two things in conjunction.
Nathan Lawrence
I agree--this is an excellent use for python. I work in publishing and we format citations in python. Advanced string formatting also comes really handy, enabling you to pass a dictionary of key-value pairs into a format string. Makes for clean code.
twneale