Related Articles

Related Articles

How to scrape all the text from body tag using Beautifulsoup in Python?
  • Last Updated : 29 Dec, 2020

strings generator is provided by Beautiful Soup which is a web scraping framework for Python. Web scraping is the process of extracting data from the website using automated tools to make the process faster. One drawback of the string attribute is that it only works for tags with string inside it and returns nothing for tags with further tags inside it. Thus to resolve this issue, a strings generator is used to get all the strings inside a tag, recursively.

Syntax:  

tag.strings 

Below given examples explain the concept of strings in Beautiful Soup. 
Example 1: In this example, we are going to get the strings.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import Beautiful Soup
from bs4 import BeautifulSoup
  
# Create the document
doc = "<body><b> Hello world </b><h1> New heading </h1><body>"
  
# Initialize the object with the document
soup = BeautifulSoup(doc, "html.parser")
  
# Get the whole body tag
tag = soup.body
  
# Print each string recursivey
for string in tag.strings:
    print(string)

chevron_right


Output: 



 Hello world 
 New heading 

Example 2:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

import requests
from bs4 import BeautifulSoup
  
# url of the website 
  
# getting response object
res = requests.get(doc)
  
# Initialize the object with the document
soup = BeautifulSoup(res.content, "html.parser")
  
# Get the whole body tag
tag = soup.body
  
# Print each string recursivey
for string in tag.strings:
    print(string)

chevron_right


Output: 
 


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :