2004年08月28日 星期六 15:29
HTMLParser和sgml/htmllib的Parser一样都是正则表达式匹配的只考虑了ascii
只好自己来处理一下
例如:
import htmllib, formatter
import re
hrefmatch = re.compile("[Hh][Rr][Ee][Ff]=(.*?)[\s>]")
class MyParser(htmllib.HTMLParser):
def __init__(self, verbose = 0):
self.anchors = {}
fmt = formatter.NullFormatter()
htmllib.HTMLParser.__init__(self, fmt, verbose)
def anchor_bgn(self, href, name, type):
self.save_bgn()
#self.anchor = href
tag_text = self.get_starttag_text()
#print tag_text
myhref = hrefmatch.search(tag_text)
if myhref:
#print myhref.groups()
self.anchor = myhref.groups()[0]
def anchor_end(self):
#print self.get_starttag_text()
tagtxt = self.save_end().strip()
if self.anchor and tagtxt:
self.anchors[tagtxt] = self.anchors.get(tagtxt, []) + [self.anchor]
## def handle_starttag(self, tag, method, attributes):
## self.save_bgn()
## print "tag=",tag,"method=",method,"attributes=", attributes
## print self.get_starttag_text( )
## #self.anchor =
fd = open("test.html", 'r')
html = fd.read()
html = unicode(html, 'mbcs')
fd.close()
#print html
parser = MyParser()
parser.feed(html)
parser.close()
for k in parser.anchors.keys():
for a in parser.anchors[k]:
print k, a
> Anew,您好!
>
> sgml的parser不能正确处理中文,所以当遇到中文的时候就自动截断了。
> 当然因为我的应用很简单,所以用正则表达式解决就可以。你的方法当然也是不错的。谢谢!
>
> ======= 2004-08-28 03:38:26 您在来信中写道:=======
>
> >dreamingk(天成),您好!
> >
> > 这几天大家讨论如何使用程序登陆论坛我很感兴趣,我前些日子就用Python来处理论坛里的文档。
> > 但我没有用ClientCookie,因为我不知道有这个东西,最近刚知道。
> > 我一直都是用Socket直接连Http的80,然后提交协议。其实很简单,你只要把IE游览这个论坛的TCP报文捕获下来,然后用Socket连就可以了。
> > 而处理HTTP我用的是一个Div Into Python里的BaseHTMLProcessor. 这样就可以基本解决大多数的HTML处理.
> > 但是像 ftp://music.popv.net:bbs.popv.net@218.104.78.34/游戏/莎木一章横须贺_Shenmue/Shenmue_OST2/船员の复响.mp3>网通公众网镜像 可能就有一点问题了(虽然我没测试,你可以去试试)
> > 但我想用正则表达式处理就可以了, 例如: .*?)>.*?
> > OK ,解决了.
> >
> >======= 2004-08-27 12:56:00 您在来信中写道:=======
> >
> >>Xie Yanbo,您好!
> >>
> >> 已经按照上述方法搞定了,可以正确登陆论坛。原来的原因是我没有用ClientCookie.urlopen()来打开网页,而是用的urllib.urlopen()也不能获得正确的网页。
> >> 但是现在也有点问题。网页上的href都是这样的
> >> ftp://music.popv.net:bbs.popv.net@218.104.78.34/游戏/莎木一章横须贺_Shenmue/Shenmue_OST2/船员の复响.mp3>网通公众网镜像
> >> 但是我用sgml的parser解析获得的href都只有前半段 ftp://music.popv.net:bbs.popv.net@218.104.78.34/ ,这个是解析器的问题吗?
> >>
> >>======= 2004-08-27 11:49:26 您在来信中写道:=======
> >>
> >>>On 2004-08-25 11:00:1093402811 +0800, dreamingk(天成) wrote:
> >>>> dreamingk(天成),您好!
> >>>>
> >>>> ClientCookie.CookieJar确实没有save 的方法
> >>>> MSIECookieJar 才有这个方法
> >>>
> >>>研究了一下新版本的 ClientCookie,还有 Python2.4 的 cookielib,
> >>>更新了一下那个登录 linuxforum 的代码,大家有兴趣可以看:
> >>>
> >>> http://xie.freezope.org/blog/2004/08/python.html
> >>>
> >>>_______________________________________________
> >>>python-chinese list
> >>>python-chinese at lists.python.cn
> >>>http://python.cn/mailman/listinfo/python-chinese
> >>>
> >>
> >>= = = = = = = = = = = = = = = = = = = =
> >>
> >>
> >> 致
> >>礼!
> >>
> >>
> >> dreamingk(天成)
> >> dreamingker at 163.com
> >> 2004-08-27
> >>
> >>_______________________________________________
> >>python-chinese list
> >>python-chinese at lists.python.cn
> >>http://python.cn/mailman/listinfo/python-chinese
> >>
> >
> >= = = = = = = = = = = = = = = = = = = =
> >
> >
> > 致
> >礼!
> >
> >
> > Anew
> > Anewboy at citiz.net
> > 2004-08-28
> >
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> >
>
> = = = = = = = = = = = = = = = = = = = =
>
>
> 致
> 礼!
>
>
> dreamingk(天成)
> dreamingker at 163.com
> 2004-08-28
>
>
-----------------------------------------------------------
马上到http://www.126.com申请260M全国最大免费邮箱!
提供新邮件到达手机短信提醒功能, 随时掌握邮件信息!
Zeuux © 2025
京ICP备05028076号