【发布时间】:2011-09-11 20:38:57
【问题描述】:
我有一个包含大约 50 个表格的 HTML 页面。每个表的布局相同,但值不同,例如:
<table align="right" class="customTableClass">
<tr align="center">
<td width="25" height="25" class="usernum">value1</td>
<td width="25" height="25" class="usernum">value2</td>
<td width="25" height="25" class="usernum">value3</td>
<td width="25" height="25" class="usernum">value4</td>
<td width="25" height="25" class="usernum">value5</td>
<td width="25" height="25" class="usernum">value6</td>
<td width="25" height="25" class="totalnum">otherVal</td>
</tr>
</table>
我的 REST 服务器 正在运行 django/python,所以在我的 urls.py 中,我正在调用我的 def parse_url(): 函数,显然我想在其中完成所有工作。我的问题是,当谈到 python 时,我几乎是一个新手,所以实际上只是不知道将我的代码放在哪里。我从HTMLParser python 文档中得到了一些代码,并将其更改如下:
import urllib, urllib2
from django.http import HttpResponse
from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print "Encountered the beginning of a %s tag" % tag
def handle_endtag(self, tag):
print "Encountered the end of a %s tag" % tag
def handle_data(self, data):
HttpResponse("Encountered data %s" % data)
def parse_url(request):
p = MyHTMLParser()
url = 'http://www.mysite.com/lists.asp'
content = urllib.urlopen(url).read()
p.feed(content)
return HttpResponse('DONE')
目前,这段代码没有输出任何有用的东西。它只是打印出DONE,这不是很有用。
handle_starttag()等类方法如何使用?当我使用p.feed(content)时不应该自动调用这些吗??
基本上,我最终想要完成的是,当我转到mysite.com/showlist 时,能够输出一个列表:
value1
value2
value3
value4
value5
value6
othervalue
这需要循环完成,因为每个表中大约有 50 个不同值的表。
感谢您帮助初学者!
【问题讨论】:
标签: python django parsing rest