2006年09月01日 星期五 17:44
python,您好!
作了一个小玩意,BeautifulSoup的wrapper。呵呵,学习加玩乐。如果谁知道有人已经做过了,请告诉我。谢谢。
可以让你按照层次取得想要的东西。
比如:
soup = mysoup.MySoup(doc)
# soup里只包含了数据(其它的从BeautifulSoup可以轻易得到)。
# soup里有一个Body成员,一切都从这里开始。(里面所有的成员都以大写字母开头,且只包含Tag)
text1 = souppBody.Table[1].Tr[1].Td[2].content.string
text2 = Body.Table[1].Tr[1].Td[3].content.contents[0].string
#其中,content是BeautifulSoup的Tag.
mysoup.py : 这个小玩意
mysoup_test.py : 一个应用的例子,取在线字典的数据,使用:python mysoup_test.py 要查的词
root$python mysoup_test.py python
python:
n. 丹舌,大蟒,巨蟒
mysoup.py ---------------------------------------------------------------------
# -*- coding: utf-8 -*-
"""
MySoup : a wrapper of BeautifulSoup
Author : Robin Zhang
Date : 2006/9/1
Example:
soup = MySoup(''.join(s))
text1 = soup.Body.Table[1].Tr[1].Td[0].Table[0].Tr[1].Td[0].Table[0].Tr[0].Td[0].Big[0].B[0].Font[0].content.string
text2 = soup.Body.Table[1].Tr[1].Td[0].Table[0].Tr[1].Td[0].Table[0].Tr[0].Td[0].Big[1].Font[0].content.contents[0].string
text3 = soup.Body.Table[1].Tr[1].Td[0].Table[0].Tr[1].Td[0].Table[0].Tr[0].Td[0].Big[1].Font[0].content.contents[2].string
"""
from BeautifulSoup import BeautifulSoup
class LayerTag:
"""
"""
def __init__(self, Tag):
"""
"""
self.content = Tag
def AddSubTag(self, SubTagName, SubLayerTag):
"""
"""
if getattr(self, SubTagName.capitalize(), None) == None:
setattr(self, SubTagName.capitalize(), [])
getattr(self, SubTagName.capitalize()).append(SubLayerTag)
class MySoup(BeautifulSoup):
"""
"""
def __init__(self, *args, **kwargs):
BeautifulSoup.__init__(self, *args, **kwargs)
self.Body = self._create_tag_layer(self.html.body)
def _create_tag_layer(self, Tag):
"""
"""
layertag = LayerTag(Tag)
for content in Tag.contents:
if content.__class__.__name__ == "Tag":
layertag.AddSubTag(content.name, self._create_tag_layer(content))
return layertag
mysoup_test.py ---------------------------------------------------------------------
# -*- coding: utf-8 -*-
"""
MySoup_test : test MySoup
Author : Robin Zhang
Date : 2006/9/1
"""
import mysoup
import urllib2,sys
if __name__ == '__main__':
"""
This example visits an on line dictionary site to consult a word
"""
url = "http://sh.dict.cn/search/?q="
if len(sys.argv) < 2:
print "Usage : mysoup new_word"
sys.exit()
try:
page = urllib2.urlopen("%s%s" % (url, sys.argv[1]))
soup = mysoup.MySoup(page).Body.Table[1].Tr[1].Td[0].Table[0].Tr[1].Td[0].Table[0].Tr[0].Td[0]
print soup.Big[0].B[0].Font[0].content.string.strip("\r\n")
for item in soup.Big[1].Font[0].content.contents:
if item.__class__.__name__!="Tag":
print item.string.strip("\r\n")
except Exception, msg:
print "Error: %s" % msg
---------------------------------------------------------------------------------
致
礼!
cry
zyqmail在tom.com
2006年09月16日 星期六 09:14
cry <zyqmail在tom.com> writes: > 作了一个小玩意,BeautifulSoup的wrapper。呵呵,学习加玩乐。如果谁知道有 > 人已经做过了,请告诉我。谢谢。 > > 可以让你按照层次取得想要的东西。 这个似乎不是很有必要吧,用 BeautifulSoup 本身就可以了。例如, f = urllib.urlopen(url) soup = BeautifulSoup(f.read()) 读取第一个 table 的第一行第一列: soup('table')[0]('tr')[0]('td')[0] -- William Zall's Laws: (1) Any time you get a mouthful of hot soup, the next thing you do will be wrong. (2) How long a minute is, depends on which side of the bathroom door you're on.
2006年09月16日 星期六 11:40
mæ®¶'îÊ.¦f¥Æf·ºØº]´Ó¯ýÿ^YbjeîÂ)e©±ÂX&j;)\¢g+Ë<ª¨¥¶¢l+׬殶'î*.§
Ú¦«æ®¶'î*.¥û«Xº¹h¥éî®[(º^jëb~éR¢ê_浦åzÊ.¦Öí-¯K]Ñh¥&¦e©e°¶°³P'ÊØ¦{*.ëZ
û¥¡øh¶Ê.¦Ø^ìm¶§*.v"VÞº'aèÂZ'©¢ë^Ç^¥éݲð'!²'^¡ûay¶º(Ú(¯*.ê'§+a¢w!w¬xú,¶Ç§vÜ)Þ±éb²Û)ÊØhÉÒ¹»®&Þ±éݲæìr¸zÚ)ÊØhÈbëê®zËeËl§+a¢w'R{.nÇ+·¬zwn˱Êâmëh§+a¢w!w¬z·ª¹ë-+-²Ü7j)Hú!¶ÚþÜùYùb²Ø§~éÊØhÈbëuëÝ¡êÝz÷hz¸&j;)\¢hm¶ÿ¦º#yËfÊØ§ÉÿrÛ'{ij»@tÌ-«miÈfz{pjË®æÛyÕ.m§ÿéÊØhÉÿ¦*^®f¢úr¶'r§zÇ¿jÛZrÛ?ÛM:ÓÝzÿ^yöúÛöµ§!éíÙ¥2006年09月16日 星期六 12:06
On 9/16/06, bird devdoer <devdoer在gmail.com> wrote: > > 没有使过beautifulsoup,问问;html一定要是合法的xml(xhtml),beautiful才能解析么 > > beautiful soup 最大的卖点就是 可以处理不怎么合规范的 html/xml 更详细的资料可以看主页: http://www.crummy.com/software/BeautifulSoup/ -- simple is good http://brucewang.net -------------- 下一部分 -------------- 一个HTML附件被移除... URL: http://python.cn/pipermail/python-chinese/attachments/20060916/2c241503/attachment.html
2006年09月16日 星期六 22:32
"bird devdoer" <devdoer在gmail.com> writes: > 没有使过beautifulsoup,问问;html一定要是合法的xml(xhtml),beautiful才能 > 解析么 beautifulsoup 和那个 tidylib 类似,就是为了将不完全合法的 xml 完善成合法 的。:-) -- William Metermaids eat their young.
Zeuux © 2025
京ICP备05028076号