Parsing HTML pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PythonNoob89
    New Member
    • Mar 2007
    • 2

    #1

    Parsing HTML pages

    Hello me and my friends are currently working on an easy pythion project but we are stuck with one of the functions we need to develope:(
    We really don't know how to parse data from an html page.. we pasted a copy of our program below and it would be great if somebody could give us some tips on how to do this:D thx alot for your time and patience.
    Code:
    import urllib
    import sys, re, os
    
    def get_page_from_file(aFile):
        imputFile = open(aFile, 'r')
        strHTML = imputFile.read()
        strHTML = re.sub(r'\s+'," " , strHTML)
        return strHTML
    
    def get_title(strHTML):
        Title_pattern = re.compile(r'<title>(.*) -- (.*)</title>', re.I)
        matchedObject = Title_pattern.search(strHTML)
        if matchedObject:
            return matchedObject.group(2)
        else:
            return "No title found."
    
    def getData(strHTML):
        pattern = r'class="CountryLink".'
        needle = re.compile(pattern)
        resultList = needle.split(strHTML)
        return resultList
    
    def parseData(strHTML):
        parse_pattern = re.compile(r'<title>(.*) -- (.*)</title>', re.I)
        matchedObject = parse_pattern.search(strHTML)
        if matchedObject:
            return matchedObject.group(2)
        else:
            return "No parseData."
    
    aFile = 'area.html'
    strHTML = get_page_from_file(aFile)
    title = get_title(strHTML)
    data = getData(strHTML)
    parse = parseData(strHTML)
    
    print parse
    print title
    print data[45]
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    basically, if you wish to do your own parsing, the tools you need are those string methods such as split,strip,rep lace,etc or the re module. But I suggest you take a look at html parser libraries such as htmllib , beautifulsoup . This dive into python page also shows how to parse html. Have a look.
    these tools make it easier to do what you want.

    Comment

    • PythonNoob89
      New Member
      • Mar 2007
      • 2

      #3
      thx u very much :D .. U really helped us get a good grade:P

      Comment

      Working...