0
<div class="friendBlockContent">
                Bartdavy<br>
                <span class="friendSmallText">
        Online
                </span>
            </div>

is the html, and I tried

 for div in soup.findAll("div", class_="friendBlockContent", ):
     print(div)

And this gives me if he's online, I only wanna get the name, how could I do this?

  • 1
    Possible duplicate of [Only extracting text from this element, not its children](http://stackoverflow.com/questions/4995116/only-extracting-text-from-this-element-not-its-children) – Dmitry Mar 09 '17 at 10:34

3 Answers3

2

div has two text node, you can access with .strings and use .stripped_strings to get clean data. then unpack the two node with name and online field.

In [50]:  for div in soup.findAll("div", class_="friendBlockContent", ):
    ...:      name, online = div.stripped_strings
    ...:     

In [51]: name
Out[51]: 'Bartdavy'

In [52]: online
Out[52]: 'Online'
宏杰李
  • 11,820
  • 2
  • 28
  • 35
1

A good way to achieve this:

for div in soup.findAll("div",class_="friendBlockContent", ):
    print(div.contents[0])
Pyglouthon
  • 552
  • 4
  • 15
0

You can use the following code if you can make sure that the structure is similar to the one you posted:

for div in soup.findAll("div", class_="friendBlockContent", ):
     print(div.contents[0].strip())
Zroq
  • 8,002
  • 3
  • 26
  • 37