views:

417

answers:

9

What language and libraries are suitable for a script to parse and download small numbers of web resources?

For example, some websites publish pseudo-podcasts, but not as proper RSS feeds; they just publish an MP3 file regularly with a web page containing the playlist. I want to write a script to run regularly and parse the relevant pages for the link and playlist info, download the MP3, and put the playlist in the MP3 tags so it shows up nicely in my iPod. There are a bunch of similar applications that I could write too.

What language would you recommend? I would like the script to run on Windows and MacOS. Here are some alternatives:

  • JavaScript. Just so I could use jQuery for the parsing. I don't know if jQuery works outside a browser though.
  • Python. Probably good library support for doing what I want. But I don't love Python syntax.
  • Ruby. I've done simple stuff (manual parsing) in Ruby before.
  • Clojure. Because I want to spend a bit of time with it.

What's your favourite language and libraries for doing this? And why? Are there any nice jQuery-like libraries for other languages?

A: 

I would probably do this with PHP, curl, & phpQuery .. but there's a lot of different ways ..

Scott Evernden
+1  A: 

I highly recommend using Ruby and the hpricot library.

Mikael S
+2  A: 

Like Mikael S has mentioned hpricot is a great ruby html parser. However, for page retrieval, you may consider using a screen scraping library like scRUBYt or Mechanize.

nan
+7  A: 

If you want to spend some time with Clojure (a very good idea IMO!), give Enlive a shot. The GitHub description reads

a selector-based (à la CSS) templating and transformation system for Clojure — Read more

In addition to being useful for templating, it's a capable webscraping library; see the initial part of this tutorial for some simple scraping examples. (The third one is the New York Times homepage, so actually not as simple as all that.)

There are other tutorials available on the Web if you look for them; Enlive itself comes with some docs / examples. (Plus the code is < 1000 lines in total and very readable, though I suppose this might be less so for someone new to the language.)

Michał Marczyk
Clojure + Enlive looks as if it will do the job and be fun too. Thanks.
Bennett McElwee
Hope you enjoy it. :-)
Michał Marczyk
Thanks for adding to http://stackoverflow.com/questions/773340/can-you-provide-an-example-of-parsing-html-with-your-favorite-parser/2389195#2389195 as well.
Bennett McElwee
+4  A: 

In ruby you also have Nokogiri, Nokogiri (鋸) is an HTML, XML, SAX, and Reader parser. Among Nokogiri's many features is the ability to search documents via XPath or CSS3 selectors.

Pedro
+5  A: 

Beautiful Soup (http://www.crummy.com/software/BeautifulSoup/) is a good python library for this. It specializes in dealing with malformed markup.

ishanrc
And as we all know *all* HTML is malformed. Seriously BS will handle almost anything you throw at it in a sensible manner.
mac
A: 

What do you really want to do? If you want to learn Clojure||ruby||C do that. If you just want to get it done do whatever is fastest for you to do. And at the very least when you say Clojure and library you are also saying Java and library, there are lots and some are very good(I don't know what they are though). And the same was said for ruby and python above. So what do you want to do?

ms4720
The question states that I want to write a script, so answers should address that (for example by mentioning some libraries by name). The fact that I am learning Clojure is obviously tangential, otherwise I wouldn't have mentioned other languages.
Bennett McElwee
A: 

For jQuery-like CSS selector library in Perl then take a look at pQuery

Also have a look at this previous SO question for examples of HTML parsing & scraping in many languages.

/I3az/

draegtun