I have a HTML page that displays a few values. I also have a little app that displays data from some other pages I have, but these other pages are JSON, not HTML. I want to consume these values from the HTML page, convert to JSON, then output.
The reason I want to do this is so that I can simply reuse my code, and just change the URL, or even dynamically create it.
I made the HTML page as plain as possible, so as to strip out all the junk in order to make the regex more basic.
Here is the HTML:
<div class="BlockA">
<h4>BlockA</h4>
<div class="name">John Smith</div>
<div class="number">2</div>
<div class="name">Paul Peterson</div>
<div class="number">14</div>
</div>
<div class="BlockB">
<h4>BlockB</h4>
<div class="name">Steve Jones</div>
<div class="number">5</div>
</div>
Both blocks will have varying numbers of elements,depending on a few factors.
Here is my python:
def index(request, toGet="xyz"):
file = urllib2.urlopen("http://www.mysite.com/mypage?data="+toGet)
data = file.read()
dom = parseString(data)
rows = dom.getElementsByTagName("BlockA")[0]
readIn = ""
for row in rows:
readIn = readIn+json.dumps(
{'name': row.getAttribute("location"),
'number': row.getAttribute("number")},
sort_keys=True,
indent=4)+","
response_generator = ( "["+readIn[:-1]+"]" )
return HttpResponse(response_generator)
So this is basically reading the values (actually, the source is XML in this case), looping through them, and outputting all the values.
If someone can point me in the right direction, it would be much appreciated. For example, reading in the tags like "BlockA" and then the tags "name" and "number".
Thanks.