beautifulsoup的find问题

1、我在编码时,当txtb只有3层的时候,编码没问题,能达到我的目的。即查找节点,如果节点不存在,则添加节点,但是当txtb有4层的时候,find就查不到节点了。不知道什么原因。
具体编码如下

from bs4 import BeautifulSoup user=cinyee
proj=Christmas txtb="<b>behance</b>"
txtb="<b>behance<tusername>cinyee</tusername></b>"
txtb="<b>behance<tusername>cinyee<tprojname>Christmas</tprojname></tusername></b>" soup = BeautifulSouptxtb, "html.parser" printsoup.prettify
print-------------begin
tuser=soup.findname="tusername",text=user
printtuser
if tuser is None: tag = soup.new_tag"tusername" tag.string = user soup.b.appendtag print--------1 else: tproj=tuser.find"tprojname",text=proj if tproj is None: tag = soup.new_tag"tprojname" tag.string = proj tuser.appendtag print--------2 else: tfile=tproj.find"tfilename",text=sfile print--------3 if tfile is None: tag = soup.new_tag"tfilename" tag.string = sfile tproj.appendtag printtuser
print-------------end printsoup.prettify

text=xxx 这个条件去掉~

from bs4 import BeautifulSoup user=cinyee
proj=Christmas txtb="<b>behance</b>"
txtb="<b>behance<tusername>cinyee</tusername></b>"
txtb="<b>behance<tusername>cinyee<tprojname>Christmas</tprojname></tusername></b>" soup = BeautifulSouptxtb, "html.parser" printsoup.prettify
print-------------begin
tuser=soup.findname="tusername"#,text=user
printtuser
##printreprtuser.text,repruser+proj
if tuser is None: tag = soup.new_tag"tusername" tag.string = user soup.b.appendtag print--------1 else: tproj=tuser.find"tprojname"#,text=proj if tproj is None: tag = soup.new_tag"tprojname" tag.string = proj tuser.appendtag print--------2 else: tfile=tproj.find"tfilename"#,text=sfile print--------3 if tfile is None: tag = soup.new_tag"tfilename" tag.string = sfile tproj.appendtag printtuser
print-------------end printsoup.prettify

用text来区分

这并不是个好主意~,通常是给标签添加属性来区分,例如:class=user

发表评论

电子邮件地址不会被公开。 必填项已用*标注