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.
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]
Comment