Advertisement
furas

BS4 - text and string

Apr 14th, 2017
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. data = '''<p class="address">
  4. <span>19649 Airway Blvd, </span>
  5. <span>California City, </span>
  6. <span>CA </span>
  7. <span>93505</span>
  8. </p>'''
  9.  
  10. from bs4 import BeautifulSoup
  11.  
  12. soup = BeautifulSoup(data, 'html.parser')
  13.  
  14. for p in soup.find_all('p', class_='address'):
  15.     print("p string:", p.string)
  16.     print("p text  :", p.text)
  17.     for s in p.find_all('span'):
  18.         print('span string:', s.string)
  19.         print('span text  :', s.text)
  20.  
  21. '''
  22. p string: None
  23. p text:
  24. 19649 Airway Blvd,
  25. California City,
  26. CA
  27. 93505
  28.  
  29. span string: 19649 Airway Blvd,
  30. span text  : 19649 Airway Blvd,
  31. span string: California City,
  32. span text  : California City,
  33. span string: CA
  34. span text  : CA
  35. span string: 93505
  36. span text  : 93505
  37. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement