2

I currently have some Ruby code used to scrape some websites. I was using Ruby because at the time I was using Ruby on Rails for a site, and it just made sense.

Now I'm trying to port this over to Google App Engine, and keep getting stuck.

I've ported Python Mechanize to work with Google App Engine, but it doesn't support DOM inspection with XPATH.

I've tried the built-in ElementTree, but it choked on the first HTML blob I gave it when it ran into '&mdash'.

Do I keep trying to hack ElementTree in there, or do I try to use something else?

thanks, Mark

John La Rooy
  • 295,403
  • 53
  • 369
  • 502
MStodd
  • 4,716
  • 3
  • 30
  • 50
  • 2
    Duplicate of all of these: http://stackoverflow.com/search?q=%5Bpython%5D+html+parse – S.Lott Oct 13 '09 at 22:02
  • I might have to go with scrapy, can i use XPath with beautiful soup? – MStodd Oct 15 '09 at 05:53
  • Actually I might have to go with none since I'm not sure beautiful soup works with xpath, and it looks like scrapy has a binary dependancy. – MStodd Oct 15 '09 at 06:00

5 Answers5

11

Beautiful Soup.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • For some reason I was thinking that was pure python, but it looks like it is. I'll check it out. – MStodd Oct 13 '09 at 22:11
  • 2
    Second that. Beautiful Soup is incredible. – David Wolever Oct 13 '09 at 22:21
  • 1
    Right answer, but there's something fundamentally broken about getting 60+ points for being the first person to write two words. ;) – Nick Johnson Oct 14 '09 at 10:18
  • 1
    @Nick Johnson: Since it's a duplicate question, it's doubly wrong to get upvoted for answering it yet again. – S.Lott Oct 14 '09 at 11:47
  • It's not really a dup. I need a pure python solution that works with XPath. I don't know that any suggestions so far meet those requirements. – MStodd Oct 15 '09 at 06:02
6

lxml -- 100x better than elementtree

Billy Joe
  • 61
  • 1
4

There's also scrapy, might be more up your alley.

Autoplectic
  • 7,566
  • 30
  • 30
0

There are a number of examples of web page scrapers written using pyparsing, such as this one (extracts all URL links from yahoo.com) and this one (for extracting the NIST NTP server addresses). Be sure to use the pyparsing helper method makeHTMLTags, instead of just hand coding "<" + Literal(tagname) + ">" - makeHTMLTags creates a very robust parser, with accommodation for extra spaces, upper/lower case inconsistencies, unexpected attributes, attribute values with various quoting styles, and so on. Pyparsing will also give you more control over special syntax issues, such as custom entities. Also it is pure Python, liberally licensed, and small footprint (a single source module), so it is easy to drop into your GAE app right in with your other application code.

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
0

BeautifulSoup is good, but its API is awkward. Try ElementSoup, which provides an ElementTree interface to BeautifulSoup.

hoju
  • 28,392
  • 37
  • 134
  • 178