Beautiful Soup Navigating Parse Tree by Going Sideways














































Beautiful Soup Navigating Parse Tree by Going Sideways



Beautiful Soup Navigating Parse Tree by Going  Sideways

Description:
                    To  Navigate the parse tree we use the Tag object, NavigableString. we see how to get all tags between tags by going down In Article Beautiful Soup Navigating Parse Tree by Going Down. we can easily Navigate Parse Tree by going Up, using .parent and .parents attributes which helps to know parent tag or all parent tag of tag. we learn the Going up Navigation In Article Beautiful Soup Navigating Parse Tree by Going Up. Now we going to see how to navigate the parse tree by going sideways.
                           Suppose, tag1 contains various tags and you know one of them now you want to know the next or previous one! . until now we retrieve tag between tag and outside tag of tag i.e parent tag but don't know how to get the next tag of another tag. 
so, the following attribute help to Navigate sideways of Parse Tree:
  • next_siblings
  • previous_sibling
  • next_siblings
  • previous_siblings
let me create simple markup as following
markup = '''
<Html>
<Head><title>Example of Paragraph tag</title></Head>  
<Body>
<!-- It is a Paragraph tag for creating the paragraph -->   
<p><b> HTML </b> stands for <i> <u> Hyper Text Markup Language. </u> </i> It is used to create a web pages and applications. This language   
is easily understandable by the user and also be modifiable. It is actually a Markup language, hence it provides a flexible way for designing the  
web pages along with the text.   
</p>  
HTML file is made up of different elements. <b> An element </b> is a collection of <i> start tag, end tag, attributes and the text between them</i>.   
</p> 
<div>
<b> 'b' tag use for bold text</b><i>important note formatted with italic tag</i><strong>strong also use to important note</strong>
<div> 
</Body>  
</Html>
'''

now import Beautiful Soup library,
from bs4 import BeautifulSoup

# create BeautifulSoup object
soup = BeautifulSoup(markup,'lxml')


next_sibling:

# let get the next sibling of tag b

print(soup.b.next_sibling)
stands for


#now retrieve the next sibling of b tag's next sibling

print(soup.b.next_sibling.next_sibling)
<i> <u> Hyper Text Markup Language. </u> </i>

previous_sibling

# now retrieve the previous sibling of b  tag
# we can see in the above code before b tag there is no tag 
# return None

print(soup.b.previous_sibling)
None


# let get the previous sibling of i to tag

print(soup.i.previous_sibling)
stands for

next_siblings

# type of next_siblings
print(type(soup.b.next_siblings))
<class 'generator'>

# let know how many tags next to b tag
for i in soup.b.next_siblings:
    print(i)
stands for
<i> <u> Hyper Text Markup Language. </u> </i>
It is used to create a web pages and applications. This language
is easily understandable by the user and also be modifiable. It is actually a Markup language, hence it provides a flexible way for designing the
web pages along with the text.



previous_siblings

# type of previous_sibling
print(type(soup.b.previous_siblings))
<class 'generator'>

# let retrieve all previous tags of strong tag
for i in soup.strong.previous_siblings:
    print(i)
<i>important note formatted with italic tag</i>
<b> 'b' tag use for bold text</b>






More Articles of AARTI SHELAR:

Name Views Likes
Python Beautiful Soup : Scrape the Rate of Movie from IMDb 98 0
Python BeautifulSoup : Scrape the Review of mask from Amazon 180 0
Python Beautiful Soup : Scrape the Redmi mobile detail from Amazon 217 0
Python BeautifulSoup: Scape Internship from Internshala 165 0
Python BeautifulSoup.Tag.find_previous() and Beautiful.Tag.find_all_previous() 20 0
Python BeautifulSoup.Tag.find_next() and BeautifulSoup.Tag.find_all_next() 18 0
Python BeautifulSoup.Tag.find_previous_siblings() and BeautifulSoup.Tag.find_previous_sibling() 27 0
Python BeautifulSoup.Tag.find_next_sibling() and BeautifulSoup.Tag.find_next_siblings() 33 0
Python BeautifulSoup.Tag.find_parent() and BeautifulSoup.Tag.find_parents() 29 0
Python BeautifulSoup.find() 23 0
Python logging.Handler.addFilter() 27 0
Python logging.Handler.removeFilter() 16 0
Beautiful Soup Searching Parse Tree with find_all() 28 0
Beautiful Soup Navigating Parse Tree by Going Back and Forth 21 0
Beautiful Soup Navigating Parse Tree by Going Sideways 23 1
Beautiful Soup Navigating Parse Tree by Going Down 15 0
Beautiful Soup Navigating Parse Tree by Going Up 11 0
Beautiful Soup with NavigableString ,comments and other special string 41 0
Beautiful Soup with tag object 25 0
Python Beautiful Soup introduction 47 0
Python logging.config.dictConfig() 38 0
Python logging.config.fileConfig() 29 0
Python configuring logging 29 0
Python logging.getLogger().hasHandlers() 31 0
Python logging.Filter() 27 0
Python logging.FileHandler() 17 0
Python logging.Handler.setFormatter() 19 0
logging logging.StreamHandler() 25 0
Python logging.Formatter() 18 0
Python logging.getLogger().removeHandler(handler) 39 0
python logging.getLogger().addHandler(handler) 16 0
Python handlers in logging 30 0
Python logging.getLogger().getChild(suffix) 21 0
Python logging.getLogger().isEnabledFor(level) 13 0
python logging.getLogger().setLevel() 20 0
Python logging.log() 25 0
Python logging.exception() 25 0
Python logging method of logger object 17 0
python logging.getLogger().getEffectivelevel() 24 1
python logging.getLogger() 23 0
Python logging.getLogRecordFactory() 27 0
Python logging.basicConfig() 46 0
Python logging introduction 135 0

Comments