0
votes

I wanted to search for a specific br tag(the last br tags in each div like text3,text6,text9) in a div :

<li class="odd"> text1 <br>   text2 <br>   text3 <br>    text4</li>
<li class="odd"> text4  <br>   text5  <br> text6 </li>
...
<li class="odd"> text7 <br>   text8  <br>  text9  <br>   text10</li>

and this the code i used:

from bs4 import BeautifulSoup
import requests
URL = '...'
content = requests.get(URL)
soup = BeautifulSoup(content.text, 'lxml')
contentTable  = soup.find_all("li", {"class": "odd"})
for li in contentTable:
    print(li.text)

and the output is this:

text1 text2 text3 text4 ...text9

now my problem is that I don't know how to only extract and find the br tags that I want.

1
Can you edit your question and put there expected output? - Andrej Kesely

1 Answers

0
votes

You can use stripped_strings instead of text:

from bs4 import BeautifulSoup
import requests
URL = '...'
content = requests.get(URL)
soup = BeautifulSoup(content.text, 'lxml')
contentTable  = soup.find_all("li", {"class": "odd"})
for li in contentTable:
    li_contents = list(li.stripped_strings) 
    print(li_contents)

Output:

['text1', 'text2', 'text3', 'text4']
['text4', 'text5', 'text6']
['text7', 'text8', 'text9', 'text10']