I want to find the span tag beween the LI tag and its attributes. Trying with beautful soap but no luck. Details of my code. Is any one point me right methodlogy
In this this code, my getId function should return me id = "0_False-2"
Any one know right method?
from BeautifulSoup import BeautifulSoup as bs
import re
html = '<ul>\
<li class="line"> </li>\
<li class="folder-open-last" id="0">\
<img style="float: left;" class="trigger" src="/media/images/spacer.gif" border="0">\
<span class="text" id="0_False">NOC</span><ul style="display: block;"><li class="line"> </li><li class="doc" id="1"><span class="active text" id="0_False-1">PNQAIPMS1</span></li><li class="line"> </li><li class="doc-last" id="2"><span class="text" id="0_False-2">PNQAIPMS2</span></li><li class="line-last"></li></ul></li><li class="line-last"></li>\
</ul>'
def getId(html, txt):
soup = bs(html)
soup.findAll('ul',recursive=False)
head = soup.contents[0]
temp = head
elements = {}
while True:
# It temp is None that means no HTML tags are available
if temp == None:
break
#print temp
if re.search('li', str( temp)) != None:
attr = str(temp.attrs).encode('ascii','ignore')
attr = attr.replace(' ', '')
attr = attr.replace('[', '')
attr = attr.replace(']', '')
attr = attr.replace(')', '')
attr = attr.replace('(', '')
attr = attr.replace('u\'', '')
attr = attr.replace('\'', '')
att开发者_开发百科r = attr.split(',')
span = str(temp.text)
if span == txt:
return attr[3]
temp = temp.next
else:
temp = temp.next
id = getId(html,"PNQAIPMS2")
print "ID = " + id
I am sure someone can show you the BS way, but here's my approach. Just plain old Python string manipulation.
html = '<ul>\
<li class="line"> </li>\
<li class="folder-open-last" id="0">\
<img style="float: left;" class="trigger" src="/media/images/spacer.gif" border="0">\
<span class="text" id="0_False">NOC</span><ul style="display: block;"><li class="line"> </li><li class="doc" id="1"><span class="active text" id="0_False-1">PNQAIPMS1</span></li><li class="line"> </li><li class="doc-last" id="2"><span class="text" id="0_False-2">PNQAIPMS2</span></li><li class="line-last"></li></ul></li><li class="line-last"></li>\
</ul>'
def getId( html, txt):
for LI in html.split("</li>"):
if "span" in LI:
for CL in LI.split("span"):
if "class" in CL and "id" in CL and "text" in CL and txt in CL:
return CL.split("id=")[-1].split('">')[0].replace('"',"")
print "id for PNQAIPMS2: " , getId(html,"PNQAIPMS2")
print "id for NOC: ",getId(html, "NOC")
print "id for PNQAIPMS1: ",getId(html, "PNQAIPMS1")
output
$ ./python.py
id for PNQAIPMS2: 0_False-2
id for NOC: 0_False
id for PNQAIPMS1: 0_False-1
精彩评论