python - Parsing <ul> tag using beautiful soup -
consider code:
divtag = soup.find_all("div", {"class":"classname"}) print divtag tag in divtag: ultag = soup.find_all("ul", {"class":"classname"}) print ultag tag in ultag: litag = soup.find_all("li", {"class":"classname"}) print litag tag in litag: ditag = soup.find_all("div", {"class":"classname"}) print ditag tag in ditag: atags = tag.find_next("a") value = atags.string print value
it prints "divtag" & "ultag". i'm sure class names right. there 7 'li' tags within 'ul' tag not print of 'li' tags. please help. in advance.
update:
<div class="classname"> <ul auto-load="true" class="classname" data-href=""> <li class="classname"> <div class="classname"><a href="">"value"</a> string <a href="">string1</a> <a class="muted"><abbr class="timeago" title=" 1 jun, 2015, 10:23 am">7 hours ago</abbr></a> </div> </li> <li> </li> </ul> </div>
i want extract "string" value within 'a' tag.
the full solution next_sibling
ultag = soup.find("ul", {"class": "classname"}) atags = ultag.find_all("a") atag in atags: sibling = atag.next_sibling siblingstring = str(sibling).strip() if len(siblingstring) > 0: print siblingstring
Comments
Post a Comment