Extracting text following a specific a-tag
Question: i have a problem extracting text from a html-code with python. The code looks
as followed:

    
    
    <div class="...">
    <br/><a href="link1.html" title="title1">anchor1</a>text1
    <br/><a href="link2.html" title="title2">anchor2</a>important text to extract
    <br/><a href="link3.html" title="title3">anchor3</a>text3
    ...
    </div>
    

I want to extract only the text that follows one specific link. I know some
words in the anchor2. Therefor it is not a problem extracting what is
inbetween the a-tags with beautiful soup 4. But after searching around quite a
bit, I found no solution to only extracting the text that follows my important
a-tag.

I hope somebody has an idea.


Answer: Find the link, for example, by title and get the
[`next_sibling`](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#next-
sibling-and-previous-sibling):

    
    
    from bs4 import BeautifulSoup
    
    
    data = """
    <div class="...">
    <br/><a href="link1.html" title="title1">anchor1</a>text1
    <br/><a href="link2.html" title="title2">anchor2</a>important text to extract
    <br/><a href="link3.html" title="title3">anchor3</a>text3
    ...
    </div>
    """
    
    soup = BeautifulSoup(data)
    print soup.find('a', title='title2').next_sibling
    

Prints:

    
    
    important text to extract
    

