Chelsea
Chelsea

Reputation: 59

Find the next span and print the text

I have this code:

   <div class="result">
    <span class="number">number</span>
   <div class="results-metadata">
    <span class="detail"><span class="unique name 1"></span> data 1</span> 
    <span class="detail"><span class="unique name 2"></span> data 2</span>
      <br/>
    <span class="detail"><span class="unique name 3"></span> data 3</span>
      <br/>
    <span class="detail"><span class="unique name 4"></span> data 4</span>
        <br/>
   </div>
      <span class="label label-default"></span>
  </div>

So I want to print the information in span class="detail". I can do this by doing soup.findAll('span', attrs={"class":"detail"}) and then iterating with a for loop to print them all. My problem is that I am writing this information into an excel sheet with xlsxwriter. I want the number to be associated with the data 1, 2, 3, and 4. The way I want to do this is by finding the span "unique name 1" and then print the next spans data (in this case data 1). I believe I can do this with xpath but have not figured out how.

Can someone help me with printing the information in class "detail" by finding class "unique name 1" and then moving to the next span and printing that information? Thanks.

Upvotes: 2

Views: 1995

Answers (3)

Shubham Jain
Shubham Jain

Reputation: 17593

You can use Xpath as below:-

//span[@class='detail'][2]/text()

Above array [2] is represent the position . change that array if you need other span data

Hope it will help you :)

Upvotes: 0

alecxe
alecxe

Reputation: 474151

Sure, in BeautifulSoup's terms, this is called the next sibling:

soup.find("span", class_="unique name 1").next_sibling.strip()

Demo:

>>> from bs4 import BeautifulSoup
>>> data = """
...    <div class="result">
...     <span class="number">number</span>
...    <div class="results-metadata">
...     <span class="detail"><span class="unique name 1"></span> data 1</span> 
...     <span class="detail"><span class="unique name 2"></span> data 2</span>
...       <br/>
...     <span class="detail"><span class="unique name 3"></span> data 3</span>
...       <br/>
...     <span class="detail"><span class="unique name 4"></span> data 4</span>
...         <br/>
...    </div>
...       <span class="label label-default"></span>
...   </div>
... """
>>> soup = BeautifulSoup(data, "html.parser")
>>> soup.find("span", class_="unique name 1").next_sibling.strip()
u'data 1'

Upvotes: 3

hr_117
hr_117

Reputation: 9627

The way I want to do this is by finding the span "unique name 1" and then print the next spans data (in this case data 1).

But "data 1" is not the next spans text but the parent spans text

You may try this xpath:

//span[span/@class='unique name 1']/text()

Upvotes: 2

Related Questions