0

I have narrowed my objective string to the following html:

<h2 class="user-name"> John Kennity <span class="top-class"><a href="http://service-web.com/2008-07-31/11" target="_blank">highest rank </a></span>
</h2>

I want to access the name John Kennith. How can I do that in beautifulsoup python ?

1
  • Have you tried soup.h2.string? Commented Feb 22, 2017 at 19:51

2 Answers 2

1
import bs4

text = '''<h2 class="user-name"> John Kennity <span class="top-class"><a href="http://service-web.com/2008-07-31/11" target="_blank">highest rank </a></span>
</h2>'''

soup = bs4.BeautifulSoup(text, 'lxml')
name, rank = soup.h2.stripped_strings

out:

'John Kennity'
Sign up to request clarification or add additional context in comments.

3 Comments

If I limit the above scenario to text = ''<h2 class="user-card-name"> John Kennity </h2>''' and then use the method name= soup.h2.stripped_strings will return <generator object stripped_strings at 0x000001C5C1FDB830> But the expected result would be the name John Kennity. Is there any difference in applying your approach ?
@Azfar Faizan soup.h2.stripped_strings this will return a generator, you can convert it to list by list(soup.h2.stripped_strings), then you can use index to access the elements in the list.
@Azfar Faizan name, rank = generator is shortcut for name = list[0], rank=list[1]
0

Hope this helps.

user_names = soup.findAll('div', {'class': 'user-name'})
for un in user_names:
   temp = un.find('h2')
   if temp:
      print temp.text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.