2006年04月07日 星期五 15:56
大家好: 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/a9c3bb5d/attachment.html
2006年04月07日 星期五 16:09
On 4/7/06, 张冰 <simpleboy.love at gmail.com> wrote: > 大家好: > > 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了! > 这不关Python 的事儿,纯是 正则表达式的事儿! http://cvs.woodpecker.org.cn/svn/woodpecker/zqlib/tangle/zoomq/H2X/Xfilter.py 是我以前 html 2 xhtml 的练习可以参考 http://cvs.woodpecker.org.cn/svn/woodpecker/zqlib/tangle/zoomq/HtmlCntFilter/cntFilter.py 是从新浪抓文章后的过滤小工具,可以参考………… > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to > python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to > python-chinese-request at lists.python.cn > Detail Info: > http://python.cn/mailman/listinfo/python-chinese > > -- """Time is unimportant, only life important! blogging : http://blog.zoomquiet.org/pyblosxom/ wiki enter: http://wiki.woodpecker.org.cn/moin/ZoomQuiet in douban: http://www.douban.com/people/zoomq/ """
2006年04月07日 星期五 16:09
from sgmllib import SGMLParserclass html2txt(SGMLParser):
def reset(self):
self.text = ''
SGMLParser.reset(self)
def handle_data(self, text):
self.text+=text
*if* __name__ == "__main__":
parser = html2txt()
text = file('test.htm','rb').read()
parser.feed(text)
parser.close
print parser.text
如果不是一定需要正则表达式的话。
如果只是取得页面内容,可以试试这个,如果有别的需求,可以看看SGMLParser。
On 4/7/06, 张冰 <simpleboy.love at gmail.com> wrote:
>
> 大家好:
>
> 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了!
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/d4d91a29/attachment.htm
2006年04月07日 星期五 16:18
try Beautifulsoap ----- Original Message ----- From: 张冰 To: python-chinese at lists.python.cn Sent: Friday, April 07, 2006 3:56 PM Subject: [python-chinese] 如何用python过滤html标签和准确的提取内容 大家好: 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了! ------------------------------------------------------------------------------ _______________________________________________ python-chinese Post: send python-chinese at lists.python.cn Subscribe: send subscribe to python-chinese-request at lists.python.cn Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn Detail Info: http://python.cn/mailman/listinfo/python-chinese -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/60d1bedc/attachment.htm
2006年04月07日 星期五 16:49
之前,我用java写过一个类似的程序,只是java的文本处理效率太低了,尤其用到正则表达式的时候,所以我尝试用python来解决类似问题。非常感谢各位的帮忙,你们提供的代码对我有很高的参考价值,谢谢了! -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/c903d8e8/attachment.html
2006年04月07日 星期五 17:32
这种方法可以提取出页面上的文本,不过好像javascript等一些客户端脚本过滤不掉。 在06-4-7,imcs ee <imcsee at gmail.com> 写道: > > from sgmllib import SGMLParser > class html2txt(SGMLParser): > def reset(self): > self.text = '' > SGMLParser.reset(self) > def handle_data(self, text): > self.text+=text > > *if* __name__ == "__main__": > parser = html2txt() > text = file('test.htm','rb').read() > parser.feed(text) > parser.close > print parser.text > > 如果不是一定需要正则表达式的话。 > 如果只是取得页面内容,可以试试这个,如果有别的需求,可以看看SGMLParser。 > > > On 4/7/06, 张冰 <simpleboy.love at gmail.com> wrote: > > > 大家好: > > 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了! > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -- I like Python & Linux. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/3ba2dcbb/attachment.html
2006年04月07日 星期五 21:35
能否给个例子看看,我对javascript不熟悉。多谢 On 4/7/06, 员旭鹏 <recordus at gmail.com> wrote: > > 这种方法可以提取出页面上的文本,不过好像javascript等一些客户端脚本过滤不掉。 > > 在06-4-7,imcs ee <imcsee at gmail.com> 写道: > > > from sgmllib import > > SGMLParser > > class html2txt(SGMLParser): > > def reset(self): > > self.text = '' > > SGMLParser.reset(self) > > def handle_data(self, text): > > self.text+=text > > > > *if* __name__ == "__main__": > > parser = html2txt() > > text = file('test.htm','rb > > ').read() > > parser.feed(text) > > parser.close > > print parser.text > > > > 如果不是一定需要正则表达式的话。 > > 如果只是取得页面内容,可以试试这个,如果有别的需求,可以看看SGMLParser。 > > > > > > On 4/7/06, 张冰 < simpleboy.love at gmail.com> wrote: > > > > > 大家好: > > > > 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了! > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to > > python-chinese-request at lists.python.cn > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > -- > I like Python & Linux. > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/80d741ab/attachment.htm
2006年04月07日 星期五 21:47
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
from sgmllib import SGMLParser
import urllib
class html2txt(SGMLParser):
def reset(self):
self.text = ''
SGMLParser.reset(self)
def handle_data(self,text):
self.text += text
def main():
txt = urllib.urlopen("http://mp3.baidu.com").read()
txt = unicode(txt,"gbk")
parser = html2txt()
parser.feed(txt)
parser.close()
print parser.text
main()
提取出来的内容就包含有javascript和样式表:
aiyi at osr:~/Programming/Python$ ./html2txt.py
百度MP3——全球最大中文MP3搜索引擎
function syn(form){
if (form.lm[7].checked){
form.tn.value="baidump3ring";
form.ct.value="285212672";
form.rn.value="";
form.action="http://mp3.baidu.com/m"
} else {
if(form.lm[0].checked){
form.rn.value="10";
form.tn.value="baidump3lyric";
form.ct.value="150994944";
form.action="http://mp3.baidu.com/m"
}else {
if(form.lm[8].checked){
form.tn.value="baidump3cal";
form.ct.value="134217728";
form.rn.value="";
form.action="http://calling.baidu.com/m"
}
else {
form.tn.value="baidump3";
form.ct.value="134217728";
form.rn.value="";
form.action="http://mp3.baidu.com/m"
}}}}
function sf(){document.f1.word.focus();}
function h(obj,url){obj.style.behavior='url
(#default#homepage)';obj.setHomePage(url);}
body{text-align:center;font-family:宋体}
p{padding:0;margin:0}
td,div{font-size:12px;vertical-align:top}
.ff{font-family:Verdana;font-size:16px}
#n {margin:0px auto;font-size:12px;padding:0px;border-bottom:1px solid
#0000CC;BACKGROUND:#EEEEEE;width:600px;height:18px}
#n li{float:left;display:block;margin:0px;padding:0px;width:67px}
#n li a{display:block;text-decoration:none;padding:4px 0px 0px
0px;margin:0px;width:100%}
#n li a:hover{text-decoration:underline;background:#FFF;padding:4px 0px 0px
0px;margin:0px}
#n li#hd{width:56px;height:18px} #n li#mr{width:85px;height:18px}
#n .now{background:#00C;color:#FFF;padding:4px 0px 0px
0px;margin:0px;font-weight:bold;}
.d{margin-left:50px;line-height:20px}
.s{color:red;font-family:arial;font-size:11px;}
.now{background:#00c;color:#fff;padding:4px 0 0;margin:0;font-weight:600}
sup{color:red;font-family:arial;font-size:11px}
b{text-decoration:none;font-size:14px;line-height:200%;font-weight:100}
#cp{font-size:12px;width:223px;clear:both;color:#77c;line-height:35px}
#cp a{color:#77c}
资 讯网 页贴 吧知 道MP3图 片更 多 >>
歌词 全部音乐 MP3 rm wma flash 其它 铃声 彩铃
搜索帮助
MP3榜单家族:MP3排行榜中文金曲榜 新歌TOP100
歌曲TOP500 歌手TOP200歌手列表
歌曲列表 影视金曲 金榜猜猜猜
相声小品荟萃
曲艺集锦New!
轻音乐New!
音乐掌门人:热门专辑TOP10 精彩专辑 人气专辑 热门分类
音乐专题:勇往直前的白羊精彩专题放送为你钟情—张国荣New!
手机娱乐: 总排行 铃声排行 彩铃排行 MP3铃声 手机彩图 手机动画 手机游戏
泛音乐联盟
把百度设为首页 |搜索风云榜 | 关于百度
Baidu 2006使用百度前必读
aiyi at osr:~/Programming/Python$
在06-4-7,imcs ee <imcsee at gmail.com> 写道:
>
> 能否给个例子看看,我对javascript不熟悉。多谢
>
>
> On 4/7/06, 员旭鹏 <recordus at gmail.com> wrote:
> >
> > 这种方法可以提取出页面上的文本,不过好像javascript等一些客户端脚本过滤不掉。
> >
> > 在06-4-7,imcs ee <imcsee at gmail.com> 写道:
> >
> > > from sgmllib import
> > > SGMLParser
> > > class html2txt(SGMLParser):
> > > def
> > > reset(self):
> > > self.text = ''
> > > SGMLParser.reset(self)
> > > def handle_data(self, text):
> > > self.text+=text
> > >
> > >
> > > *if* __name__ == "__main__":
> > > parser = html2txt()
> > > text = file(
> > > 'test.htm','rb
> > >
> > > ').read()
> > > parser.feed(text)
> > > parser.close
> > > print parser.text
> > >
> > > 如果不是一定需要正则表达式的话。
> > > 如果只是取得页面内容,可以试试这个,如果有别的需求,可以看看SGMLParser。
> > >
> > >
> > > On 4/7/06, 张冰 < simpleboy.love at gmail.com> wrote:
> > >
> > > > 大家好:
> > >
> > > 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了!
> > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> > --
> > I like Python & Linux.
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
--
I like Python & Linux.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/464aadd3/attachment-0001.html
2006年04月07日 星期五 22:45
要过滤样式表和jvscript也很简单
import htmllib
import formatter
class MyParser(htmllib.HTMLParser):
def __init__(self, verbose = 0):
fmt = formatter.AbstractFormatter(formatter.DumbWriter())
htmllib.HTMLParser.__init__(self, fmt, verbose)
def start_style(self, attrs):
self.save_bgn()
def end_style(self):
self.save_end()
def start_script(self, attrs):
if "src" in attrs[0]:
pass
else:
self.save_bgn()
def end_script(self):
if self.savedata:
self.save_end()
if __name__== "__main__":
import sys
import urllib
f = urllib.urlopen(sys.argv[1])
# f = file("e:/test.html")
html = f.read()
f.close()
par = MyParser()
par.feed(html)
par.close()
2006/4/7, 员旭鹏 <recordus at gmail.com>:
>
> #!/usr/bin/env python
> # -*- encoding:utf-8 -*-
>
> from sgmllib import SGMLParser
> import urllib
>
>
> class html2txt(SGMLParser):
> def reset(self):
> self.text = ''
> SGMLParser.reset(self)
>
> def handle_data(self,text):
> self.text += text
>
> def main():
> txt = urllib.urlopen("http://mp3.baidu.com").read()
> txt = unicode(txt,"gbk")
> parser = html2txt()
> parser.feed(txt)
>
> parser.close()
> print parser.text
>
> main()
>
> 提取出来的内容就包含有javascript和样式表:
>
> aiyi at osr:~/Programming/Python$ ./html2txt.py
> 百度MP3――全球最大中文MP3搜索引擎
>
>
> function syn(form){
> if (form.lm[7].checked){
> form.tn.value="baidump3ring";
> form.ct.value="285212672";
> form.rn.value="";
> form.action="http://mp3.baidu.com/m"
> } else {
> if(form.lm[0].checked){
> form.rn.value="10";
> form.tn.value="baidump3lyric";
> form.ct.value="150994944";
> form.action="http://mp3.baidu.com/m"
> }else {
> if(form.lm[8].checked){
> form.tn.value="baidump3cal";
> form.ct.value="134217728";
> form.rn.value="";
> form.action="http://calling.baidu.com/m"
> }
> else {
> form.tn.value="baidump3";
> form.ct.value="134217728";
> form.rn.value="";
> form.action="http://mp3.baidu.com/m"
> }}}}
> function sf(){document.f1.word.focus();}
> function h(obj,url){obj.style.behavior='url
> (#default#homepage)';obj.setHomePage(url);}
>
>
> body{text-align:center;font-family:宋体}
> p{padding:0;margin:0}
> td,div{font-size:12px;vertical-align:top}
> .ff{font-family:Verdana;font-size:16px}
> #n {margin:0px auto;font-size:12px;padding:0px;border-bottom:1px solid
> #0000CC;BACKGROUND:#EEEEEE;width:600px;height:18px}
> #n li{float:left;display:block;margin:0px;padding:0px;width:67px}
> #n li a{display:block;text-decoration:none;padding:4px 0px 0px
> 0px;margin:0px;width:100%}
> #n li a:hover{text-decoration:underline;background:#FFF;padding:4px 0px
> 0px 0px;margin:0px}
> #n li#hd{width:56px;height:18px} #n li#mr{width:85px;height:18px}
> #n .now{background:#00C;color:#FFF;padding:4px 0px 0px
> 0px;margin:0px;font-weight:bold;}
> .d{margin-left:50px;line-height:20px}
> .s{color:red;font-family:arial;font-size:11px;}
> .now{background:#00c;color:#fff;padding:4px 0 0;margin:0;font-weight:600}
>
> sup{color:red;font-family:arial;font-size:11px}
> b{text-decoration:none;font-size:14px;line-height:200%;font-weight:100}
> #cp{font-size:12px;width:223px;clear:both;color:#77c;line-height:35px}
> #cp a{color:#77c}
>
>
>
>
> 资 讯网 页贴 吧知 道MP3图 片更 多 >>
>
>
>
>
>
>
>
>
>
> 歌词 全部音乐 MP3 rm wma flash 其它 铃声 彩铃
> 搜索帮助
>
> MP3榜单家族:MP3排行榜中文金曲榜 新歌TOP100
> 歌曲TOP500 歌手TOP200歌手列表
> 歌曲列表 影视金曲 金榜猜猜猜
> 相声小品荟萃
> 曲艺集锦New!
> 轻音乐New!
> 音乐掌门人:热门专辑TOP10 精彩专辑 人气专辑 热门分类
>
> 音乐专题:勇往直前的白羊精彩专题放送为你钟情―张国荣New!
>
>
> 手机娱乐: 总排行 铃声排行 彩铃排行 MP3铃声 手机彩图 手机动画 手机游戏
> 泛音乐联盟
> 把百度设为首页 |搜索风云榜 | 关于百度
> Baidu 2006使用百度前必读
> aiyi at osr:~/Programming/Python$
>
>
>
> 在06-4-7, imcs ee <imcsee at gmail.com> 写道:
> >
> > 能否给个例子看看,我对javascript不熟悉。多谢
> >
> >
> > On 4/7/06, 员旭鹏 <recordus at gmail.com> wrote:
> > >
> > > 这种方法可以提取出页面上的文本,不过好像javascript等一些客户端脚本过滤不掉。
> > >
> > > 在06-4-7,imcs ee <imcsee at gmail.com> 写道:
> > >
> > > > from sgmllib import
> > > >
> > > > SGMLParser
> > > > class html2txt(SGMLParser):
> > > > def
> > > >
> > > > reset(self):
> > > > self.text = ''
> > > > SGMLParser.reset(self)
> > > > def handle_data(self, text):
> > > > self.text+=text
> > > >
> > > >
> > > >
> > > > *if* __name__ == "__main__":
> > > > parser = html2txt()
> > > > text = file(
> > > >
> > > > 'test.htm','rb
> > > >
> > > >
> > > > ').read()
> > > > parser.feed(text)
> > > > parser.close
> > > > print parser.text
> > > >
> > > > 如果不是一定需要正则表达式的话。
> > > > 如果只是取得页面内容,可以试试这个,如果有别的需求,可以看看SGMLParser。
> > > >
> > > >
> > > > On 4/7/06, 张冰 < simpleboy.love at gmail.com> wrote:
> > > >
> > > > > 大家好:
> > > >
> > > > 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了!
> > > >
> > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request at lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request at lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > >
> > > --
> > > I like Python & Linux.
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> I like Python & Linux.
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
--
Andelf
BLOG:http://blog.sohu.com/members/andelf/
BLOG:http://spaces.msn.com/members/andelf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060407/bc5f9ff1/attachment-0001.htm
2006年04月08日 星期六 09:58
多谢两位,确实是把script和style里面的东西也当作正文取出来了,没有接触过HTMLParser,看了一下,好像是专门作了对这两种情况的处理。 我自己的需求是只需要获得正文,所以就把head里面的东西都过滤掉了....代码贴在下面。 On 4/7/06, Andelf <andelf at gmail.com> wrote: > > 要过滤样式表和jvscript也很简单 > import htmllib > import formatter > class MyParser(htmllib.HTMLParser): > > def __init__(self, verbose = 0): > fmt = formatter.AbstractFormatter(formatter.DumbWriter()) > htmllib.HTMLParser.__init_ _(self, fmt, verbose) > def start_style(self, attrs): > self.save_bgn() > def end_style(self): > self.save_end() > def start_script(self, attrs): > if "src" in attrs[0]: > pass > else: > self.save_bgn() > def end_script(self): > if self.savedata: > self.save_end() > > if __name__== "__main__": > import sys > import urllib > f = urllib.urlopen(sys.argv[1]) > > # f = file("e:/test.html") > html = f.read() > f.close() > par = MyParser() > par.feed(html) > par.close() > > > 2006/4/7, 员旭鹏 <recordus at gmail.com>: > > > #!/usr/bin/env python > > # -*- encoding:utf-8 -*- > > > > from sgmllib import SGMLParser > > import urllib > > > > > > class html2txt(SGMLParser): > > def reset(self): > > self.text = '' > > SGMLParser.reset(self) > > > > def handle_data(self,text): > > self.text += text > > > > def main(): > > txt = urllib.urlopen("http://mp3.baidu.com").read() > > txt = unicode(txt,"gbk") > > parser = html2txt() > > parser.feed(txt) > > > > parser.close() > > print parser.text > > > > main() > > > > 提取出来的内容就包含有javascript和样式表: > > > > aiyi at osr:~/Programming/Python$ ./html2txt.py > > 百度MP3――全球最大中文MP3搜索引擎 > > > > > > function syn(form){ > > if (form.lm[7].checked){ > > form.tn.value="baidump3ring"; > > form.ct.value="285212672"; > > form.rn.value=""; > > form.action="http://mp3.baidu.com/m" > > } else { > > if(form.lm[0].checked){ > > form.rn.value="10"; > > form.tn.value="baidump3lyric"; > > form.ct.value="150994944"; > > form.action=" http://mp3.baidu.com/m" > > }else { > > if(form.lm[8].checked){ > > form.tn.value="baidump3cal"; > > form.ct.value="134217728"; > > form.rn.value=""; > > form.action=" http://calling.baidu.com/m" > > } > > else { > > form.tn.value="baidump3"; > > form.ct.value="134217728"; > > form.rn.value=""; > > form.action=" http://mp3.baidu.com/m" > > }}}} > > function sf(){document.f1.word.focus ();} > > function h(obj,url){obj.style.behavior='url > > (#default#homepage)';obj.setHomePage(url);} > > > > > > body{text-align:center;font-family:宋体} > > p{padding:0;margin:0} > > td,div{font-size:12px;vertical-align:top} > > .ff{font-family:Verdana;font-size:16px} > > #n {margin:0px auto;font-size:12px;padding:0px;border-bottom:1px solid > > #0000CC;BACKGROUND:#EEEEEE;width:600px;height:18px} > > #n li{float:left;display:block;margin:0px;padding:0px;width:67px} > > #n li a{display:block;text-decoration:none;padding:4px 0px 0px > > 0px;margin:0px;width:100%} > > #n li a:hover{text-decoration:underline;background:#FFF;padding:4px 0px > > 0px 0px;margin:0px} > > #n li#hd{width:56px;height:18px} #n li#mr{width:85px;height:18px} > > #n .now{background:#00C;color:#FFF;padding:4px 0px 0px > > 0px;margin:0px;font-weight:bold;} > > .d{margin-left:50px;line-height:20px} > > .s{color:red;font-family:arial;font-size:11px;} > > .now{background:#00c;color:#fff;padding:4px 0 > > 0;margin:0;font-weight:600} > > sup{color:red;font-family:arial;font-size:11px} > > b{text-decoration:none;font-size:14px;line-height:200%;font-weight:100} > > #cp{font-size:12px;width:223px;clear:both;color:#77c;line-height:35px} > > #cp a{color:#77c} > > > > > > > > > > 资 讯网 页贴 吧知 道MP3图 片更 多 >> > > > > > > > > > > > > > > > > > > > > 歌词 全部音乐 MP3 rm wma flash 其它 铃声 彩铃 > > 搜索帮助 > > > > MP3榜单家族:MP3排行榜中文金曲榜 新歌TOP100 > > 歌曲TOP500 歌手TOP200歌手列表 > > 歌曲列表 影视金曲 金榜猜猜猜 > > 相声小品荟萃 > > 曲艺集锦New! > > 轻音乐New! > > 音乐掌门人:热门专辑TOP10 精彩专辑 人气专辑 热门分类 > > > > 音乐专题:勇往直前的白羊精彩专题放送为你钟情―张国荣New! > > > > > > 手机娱乐: 总排行 铃声排行 彩铃排行 MP3铃声 手机彩图 手机动画 手机游戏 > > 泛音乐联盟 > > 把百度设为首页 |搜索风云榜 | 关于百度 > > Baidu 2006使用百度前必读 > > aiyi at osr:~/Programming/Python$ > > > > > > > > 在06-4-7, imcs ee <imcsee at gmail.com> 写道: > > > > > > 能否给个例子看看,我对javascript不熟悉。多谢 > > > > > > > > > On 4/7/06, 员旭鹏 <recordus at gmail.com> wrote: > > > > > > > > 这种方法可以提取出页面上的文本,不过好像javascript等一些客户端脚本过滤不掉。 > > > > > > > > 在06-4-7,imcs ee <imcsee at gmail.com> 写道: > > > > > > > > > from sgmllib import > > > > > > > > > > SGMLParser > > > > > class html2txt(SGMLParser): > > > > > def > > > > > > > > > > > > > > > reset(self): > > > > > self.text = '' > > > > > SGMLParser.reset(self) > > > > > def handle_data(self, text): > > > > > self.text+=text > > > > > > > > > > > > > > > > > > > > > > > > > *if* __name__ == "__main__": > > > > > parser = html2txt() > > > > > text = file( > > > > > > > > > > > > > > > 'test.htm','rb > > > > > > > > > > > > > > > > > > > > ').read() > > > > > parser.feed(text) > > > > > parser.close > > > > > print parser.text > > > > > > > > > > 如果不是一定需要正则表达式的话。 > > > > > 如果只是取得页面内容,可以试试这个,如果有别的需求,可以看看SGMLParser。 > > > > > > > > > > > > > > > On 4/7/06, 张冰 < simpleboy.love at gmail.com> wrote: > > > > > > > > > > > 大家好: > > > > > > > > > > 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了! > > > > > > > > > > > > > > > _______________________________________________ > > > > > python-chinese > > > > > Post: send python-chinese at lists.python.cn > > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > > > > > > > > Unsubscribe: send unsubscribe to > > > > > python-chinese-request at lists.python.cn > > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > > > > > > > > _______________________________________________ > > > > > python-chinese > > > > > Post: send python-chinese at lists.python.cn > > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > > > > > > > > Unsubscribe: send unsubscribe to > > > > > python-chinese-request at lists.python.cn > > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > > > > > > > > > -- > > > > I like Python & Linux. > > > > > > > > _______________________________________________ > > > > python-chinese > > > > Post: send python-chinese at lists.python.cn > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > > Unsubscribe: send unsubscribe to > > > > python-chinese-request at lists.python.cn > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > > > _______________________________________________ > > > python-chinese > > > Post: send python-chinese at lists.python.cn > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > > Unsubscribe: send unsubscribe to > > > python-chinese-request at lists.python.cn > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > > > > > > -- > > I like Python & Linux. > > > > _______________________________________________ > > python-chinese > > Post: send python-chinese at lists.python.cn > > Subscribe: send subscribe to python-chinese-request at lists.python.cn > > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese > > > > > > > -- > Andelf > BLOG:http://blog.sohu.com/members/andelf/ > BLOG:http://spaces.msn.com/members/andelf > > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese #!/usr/bin/env python# -*- encoding:utf-8 -*-from sgmllib import SGMLParserimport urllibclass html2txt(SGMLParser): def reset(self): self.text = '' self.inbody = True SGMLParser.reset(self) def handle_data(self,text): *if* self.inbody: self.text += text def start_head(self,text): self.inbody = False def end_head(self): self.inbody = Truedef main(): txt = urllib.urlopen("http://mp3.baidu.com").read() #txt = unicode(txt,"gbk") parser = html2txt() parser.feed(txt) parser.close() print parser.text main() -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060408/e02f144a/attachment.htm
2006年04月08日 星期六 10:01
我贴的代码怎么自己不会分行的...
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
from sgmllib import SGMLParser
import urllib
class html2txt(SGMLParser):
def reset(self):
self.text = ''
self.inbody = True
SGMLParser.reset(self)
def handle_data(self,text):
if self.inbody:
self.text += text
def start_head(self,text):
self.inbody = False
def end_head(self):
self.inbody = True
def main():
txt = urllib.urlopen("http://mp3.baidu.com").read()
#txt = unicode(txt,"gbk")
parser = html2txt()
parser.feed(txt)
parser.close()
print parser.text
main()
On 4/8/06, imcs ee <imcsee at gmail.com> wrote:
>
>
> 多谢两位,确实是把script和style里面的东西也当作正文取出来了,没有接触过HTMLParser,看了一下,好像是专门作了对这两种情况的处理。
> 我自己的需求是只需要获得正文,所以就把head里面的东西都过滤掉了....代码贴在下面。
>
> On 4/7/06, Andelf <andelf at gmail.com> wrote:
>
> > 要过滤样式表和jvscript也很简单
> import htmllib
> import formatter
> class MyParser(htmllib.HTMLParser):
>
> def __init__(self, verbose = 0):
> fmt = formatter.AbstractFormatter(formatter.DumbWriter())
> htmllib.HTMLParser.__init _ _(self, fmt, verbose)
> def start_style(self, attrs):
> self.save_bgn()
> def end_style(self):
> self.save_end()
> def start_script(self, attrs):
> if "src" in attrs[0]:
> pass
> else:
> self.save_bgn()
> def end_script(self):
> if self.savedata:
> self.save_end()
>
> if __name__== "__main__":
> import sys
> import urllib
> f = urllib.urlopen(sys.argv[1])
>
> # f = file("e:/test.html")
> html = f.read()
> f.close()
> par = MyParser()
> par.feed(html)
> par.close()
>
>
> 2006/4/7, 员旭鹏 <recordus at gmail.com>:
>
> > #!/usr/bin/env python
> > # -*- encoding:utf-8 -*-
> >
> > from sgmllib import SGMLParser
> > import urllib
> >
> >
> > class html2txt(SGMLParser):
> > def reset(self):
> > self.text = ''
> > SGMLParser.reset(self)
> >
> > def handle_data(self,text):
> > self.text += text
> >
> > def main():
> > txt = urllib.urlopen("http://mp3.baidu.com").read()
> > txt = unicode(txt,"gbk")
> > parser = html2txt()
> > parser.feed(txt)
> >
> > parser.close()
> > print parser.text
> >
> > main()
> >
> > 提取出来的内容就包含有javascript和样式表:
> >
> > aiyi at osr:~/Programming/Python$ ./html2txt.py
> > 百度MP3――全球最大中文MP3搜索引擎
> >
> >
> > function syn(form){
> > if (form.lm[7].checked){
> > form.tn.value="baidump3ring";
> > form.ct.value="285212672";
> > form.rn.value="";
> > form.action="http://mp3.baidu.com/m"
> > } else {
> > if(form.lm[0].checked){
> > form.rn.value="10";
> > form.tn.value="baidump3lyric";
> > form.ct.value="150994944";
> > form.action=" http://mp3.baidu.com/m"
> > }else {
> > if(form.lm[8].checked){
> > form.tn.value="baidump3cal";
> > form.ct.value="134217728";
> > form.rn.value="";
> > form.action=" http://calling.baidu.com/m"
> > }
> > else {
> > form.tn.value="baidump3";
> > form.ct.value="134217728";
> > form.rn.value="";
> > form.action=" http://mp3.baidu.com/m"
> > }}}}
> > function sf(){document.f1.word.focus ();}
> > function h(obj,url){obj.style.behavior='url
> > (#default#homepage)';obj.setHomePage(url);}
> >
> >
> > body{text-align:center;font-family:宋体}
> > p{padding:0;margin:0}
> > td,div{font-size:12px;vertical-align:top}
> > .ff{font-family:Verdana;font-size:16px}
> > #n {margin:0px auto;font-size:12px;padding:0px;border-bottom:1px solid
> > #0000CC;BACKGROUND:#EEEEEE;width:600px;height:18px}
> > #n li{float:left;display:block;margin:0px;padding:0px;width:67px}
> > #n li a{display:block;text-decoration:none;padding:4px 0px 0px
> > 0px;margin:0px;width:100%}
> > #n li a:hover{text-decoration:underline;background:#FFF;padding:4px 0px
> > 0px 0px;margin:0px}
> > #n li#hd{width:56px;height:18px} #n li#mr{width:85px;height:18px}
> > #n .now{background:#00C;color:#FFF;padding:4px 0px 0px
> > 0px;margin:0px;font-weight:bold;}
> > .d{margin-left:50px;line-height:20px}
> > .s{color:red;font-family:arial;font-size:11px;}
> > .now{background:#00c;color:#fff;padding:4px 0
> > 0;margin:0;font-weight:600}
> > sup{color:red;font-family:arial;font-size:11px}
> > b{text-decoration:none;font-size:14px;line-height:200%;font-weight:100}
> > #cp{font-size:12px;width:223px;clear:both;color:#77c;line-height:35px}
> > #cp a{color:#77c}
> >
> >
> >
> >
> > 资 讯网 页贴 吧知 道MP3图 片更 多 >>
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > 歌词 全部音乐 MP3 rm wma flash 其它 铃声 彩铃
> > 搜索帮助
> >
> > MP3榜单家族:MP3排行榜中文金曲榜 新歌TOP100
> > 歌曲TOP500 歌手TOP200歌手列表
> > 歌曲列表 影视金曲 金榜猜猜猜
> > 相声小品荟萃
> > 曲艺集锦New!
> > 轻音乐New!
> > 音乐掌门人:热门专辑TOP10 精彩专辑 人气专辑 热门分类
> >
> > 音乐专题:勇往直前的白羊精彩专题放送为你钟情―张国荣New!
> >
> >
> > 手机娱乐: 总排行 铃声排行 彩铃排行 MP3铃声 手机彩图 手机动画 手机游戏
> > 泛音乐联盟
> > 把百度设为首页 |搜索风云榜 | 关于百度
> > Baidu 2006使用百度前必读
> > aiyi at osr:~/Programming/Python$
> >
> >
> >
> > 在06-4-7, imcs ee <imcsee at gmail.com> 写道:
> > >
> > > 能否给个例子看看,我对javascript不熟悉。多谢
> > >
> > >
> > > On 4/7/06, 员旭鹏 <recordus at gmail.com> wrote:
> > > >
> > > > 这种方法可以提取出页面上的文本,不过好像javascript等一些客户端脚本过滤不掉。
> > > >
> > > > 在06-4-7,imcs ee <imcsee at gmail.com> 写道:
> > > >
> > > > > from sgmllib import
> > > > >
> > > > > SGMLParser
> > > > > class html2txt(SGMLParser):
> > > > > def
> > > > >
> > > > >
> > > > >
> > > > > reset(self):
> > > > > self.text = ''
> > > > > SGMLParser.reset(self)
> > > > > def handle_data(self, text):
> > > > > self.text+=text
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > *if* __name__ == "__main__":
> > > > > parser = html2txt()
> > > > > text = file(
> > > > >
> > > > >
> > > > >
> > > > > 'test.htm','rb
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > ').read()
> > > > > parser.feed(text)
> > > > > parser.close
> > > > > print parser.text
> > > > >
> > > > > 如果不是一定需要正则表达式的话。
> > > > > 如果只是取得页面内容,可以试试这个,如果有别的需求,可以看看SGMLParser。
> > > > >
> > > > >
> > > > > On 4/7/06, 张冰 < simpleboy.love at gmail.com> wrote:
> > > > >
> > > > > > 大家好:
> > > > >
> > > > > 我是个python新手,最近写了一个python小程序。主要功能就是用python获取某个动态网页的数据流,然后提取所需要的内容,最后存入数据库。我现在遇到的问题是在提取所需内容时,如何合理的使用正则表达式,比如完全的过滤标签以及准确提取内容等等,现在程序就剩这个问题没有很好的解决了,请问各位python高手,有没有好的方法!先谢了!
> > > > >
> > > > >
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese at lists.python.cn
> > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > >
> > > > > Unsubscribe: send unsubscribe to
> > > > > python-chinese-request at lists.python.cn
> > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > _______________________________________________
> > > > > python-chinese
> > > > > Post: send python-chinese at lists.python.cn
> > > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > >
> > > > > Unsubscribe: send unsubscribe to
> > > > > python-chinese-request at lists.python.cn
> > > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > > >
> > > > >
> > > >
> > > >
> > > > --
> > > > I like Python & Linux.
> > > >
> > > > _______________________________________________
> > > > python-chinese
> > > > Post: send python-chinese at lists.python.cn
> > > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > > Unsubscribe: send unsubscribe to
> > > > python-chinese-request at lists.python.cn
> > > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > > >
> > > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> > >
> >
> >
> > --
> > I like Python & Linux.
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> >
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> Andelf
> BLOG:http://blog.sohu.com/members/andelf/
> BLOG:http://spaces.msn.com/members/andelf
>
> _______________________________________________
>
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
> #!/usr/bin/env python # -*- encoding:utf-8 -*- from sgmllib importSGMLParser
> import urllib class html2txt(SGMLParser): def reset(self): self.text = ''
> self.inbody = True
> SGMLParser.reset(self) def handle_data
> (self,text): *if* self.inbody: self.text += text def start_head(self,text):
> self.inbody = False def end_head(self): self.inbody = True
> def main(): txt = urllib.urlopen("http://mp3.baidu.com ").read() #txt =
> unicode(txt,"gbk") parser = html2txt() parser.feed(txt) parser.close()
> print parser.text main()
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060408/77b9a219/attachment.html
2006年04月08日 星期六 14:09
嗯,很好,呵呵, 没有研究过SGMLParser,功能还挺强的,呵呵 不知道你贴的怎么自己不会分行, 我是在gnome-terminal里使用vi打开直接复制了粘贴过来的 在06-4-8,imcs ee <imcsee at gmail.com> 写道: > > 我贴的代码怎么自己不会分行的... > > #!/usr/bin/env python > # -*- encoding:utf-8 -*- > from sgmllib import SGMLParser > import urllib > class html2txt(SGMLParser): > def reset(self): > self.text = '' > self.inbody = True > SGMLParser.reset(self) > def handle_data(self,text): > if self.inbody: > self.text += text > > def start_head(self,text): > self.inbody = False > def end_head(self): > self.inbody = True > def main(): > txt = urllib.urlopen("http://mp3.baidu.com").read () > #txt = unicode(txt,"gbk") > parser = html2txt() > parser.feed(txt) > parser.close() > print parser.text > main() > -- I like Python & Linux. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060408/fb377d62/attachment.html
2006年04月08日 星期六 19:13
我是按照htmllib.py里对tag的处理,子类中加入了4个方法,然后就可以处理
2006年04月09日 星期日 09:15
SGMLParser里面也是类似的处理方法。今天看到一个 pullparser 的,看了他提供的两个例子,应该也比较容易实现类似功能。 http://wwwsearch.sourceforge.net/pullparser/ On 4/8/06, Andelf <andelf at gmail.com> wrote: > > 我是按照htmllib.py里对tag的处理,子类中加入了4个方法,然后就可以处理
Zeuux © 2025
京ICP备05028076号