Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Beautiful Soup
  • Selenium
  • Scrapy
  • urllib
  • Request
  • open cv
  • Data analysis
  • Machine learning
  • NLP
  • Deep learning
  • Data Science
  • Interview question
  • ML math
  • ML Projects
  • ML interview
  • DL interview
Open In App
Next Article:
How to get text of a tag in selenium - Python?
Next article icon

How to Scrape Text from Tag in Python

Last Updated : 03 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to scrape text data from <strong> tag. We will scrape all the data which comes under the strong tag of a website. We will cover all the basic understandings with clear and concise examples.

Scraping Text from Tag

Scraping text from HTML tags can be easily done by using:

  • Selenium
  • BeautifulSoup along with requests library.

In Selenium, we will simply use the By class to get all the <strong> tag data. After acquiring the data, simply use a for loop to display the data from the <strong> tag. In the second method, we need to request the HTML content from the webpage. We will use the request library to do so. After getting the data we will parse it BeautifulScoup library of Python and at the end, we will display it.

Scraping Text from Tag using Selenium

In this, we will use selenium to fetch the text data from the <strong> tag. Let's see the code implementation.

Example

  1. By class, selenium will provide us with the set of attributes that will help us locate the web elements.
  2. Chrome Configuration options:-
    • headless option will allow us to operate Chrome without GUI.
    • sandboxing : sandboxing sandboxing problems of some websites.
    • –disable-dev-shm-usage will disable /dev/shm/ file.
  3. Then, we will provide our desired website to the web driver object and display the data under the strong tag using a for loop.
  4. At the end, we will close the browser.
Python
from selenium import webdriver from selenium.webdriver.common.by import By  options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage')  dr = webdriver.Chrome(options=options) dr.get("https://www.geeksforgeeks.org/machine-learning-types-of-artificial-intelligence/")  st = dr.find_elements(By.TAG_NAME, 'strong') for i in st:   print(i.text) dr.quit() 

Output

web_scape
Selenium web scrape text

Scraping Text from Tag using BeautifulScoup

In this, we are going to scrape the test from <strong> tag using Python's request library and beautiful soup.

Example

We will first import all the installed libraries in our code. Then we will fetch the HTML content from the webpage using the requests module of Python. After getting the data we will parse it BeautifulScoup library of Python and at the end, we will display it. We will use the find_all() function to find the text under the <strong> tag. We will finally display our acquired data and exit from our defined function.

Python
#importing necessary libraries import requests from bs4 import BeautifulSoup  #creating a function in which we will accept the url and #fetch the html content from the url using request and apply the parser function on it def strongText(url):     r = requests.get(url)      TextData = BeautifulSoup(r.content, 'html.parser')      st = TextData.find_all('strong')      #displaying the data     for data in st:         print(data.text)          if __name__ == "__main__":      #input url     url = 'https://www.geeksforgeeks.org/machine-learning-types-of-artificial-intelligence/'        #function calling     strongText(url) 

Output

web_scape01
BeautifulScoup web scrape text

Best Practices of Web Scraping

  • Follow ethical rules while scraping the data. Do not scrape any sensitive or private information from any website.
  • To store the scraped data, use efficient data storage techniques such as databases or structured files such as CSV or JSON.
  • Make sure that websites do not block you. You can use randomized intervals or proxies to avoid getting blocked.
  • Prepare your code in such a way that it can handle errors like error 404(page not found).
  • Do not overload the server by making too many requests in a short period. Keep some delays between your requests.
  • Make sure you follow the guidelines suggested by the websites before scraping the data.

Conclusion

Web Scraping is an efficient way to scrape the desired data from our provided website. We can scrape text, files, links, and many more. Although, we need to consider some ethical rules before scraping the data. We have covered, how we can scrape the data from <strong> tag. We have shown two methods to perform this task. In the first method, we used selenium to scrape the data whereas, in the second method, we used BeautifulScoup along with the requests library of Python. In both of these methods, we have demonstrated a clear and concise way to scrape the data efficiently.


Next Article
How to get text of a tag in selenium - Python?

V

vishuvaishnav3001
Improve
Article Tags :
  • Python
  • Software Testing
  • Selenium
  • Python-selenium
  • Web-scraping
  • Python BeautifulSoup
Practice Tags :
  • python

Similar Reads

  • How to get text of a tag in selenium - Python?
    Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python. In this article, we will w
    1 min read
  • How To Follow Links With Python Scrapy ?
    In this article, we will use Scrapy, for scraping data, presenting on linked webpages, and, collecting the same. We will scrape data from the website 'https://quotes.toscrape.com/'. Creating a Scrapy Project Scrapy comes with an efficient command-line tool, also called the 'Scrapy tool'. Commands ar
    8 min read
  • How to scrape all the text from body tag using Beautifulsoup in Python?
    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 an
    2 min read
  • How to Build Web scraping bot in Python
    In this article, we are going to see how to build a web scraping bot in Python. Web Scraping is a process of extracting data from websites. A Bot is a piece of code that will automate our task. Therefore, A web scraping bot is a program that will automatically scrape a website for data, based on our
    8 min read
  • How to Scrape Videos using Python ?
    Prerequisite: requestsBeautifulSoup In this article, we will discuss web scraping of videos using python. For web scraping, we will use requests and BeautifulSoup Module in Python. The requests library is an integral part of Python for making HTTP requests to a specified URL. Whether it be REST APIs
    2 min read
  • How to scrape the web with Playwright in Python
    In this article, we will discuss about Playwright framework, Its feature, the advantages of Playwright, and the Scraping of a basic webpage. The playwright is a framework for Web Testing and Automation. It is a fairly new web testing tool from Microsoft introduced to let users automate webpages more
    3 min read
  • Parsel: How to Extract Text From HTML in Python
    Parsel is a Python library used for extracting data from HTML and XML documents. It provides tools for parsing, navigating, and extracting information using CSS selectors and XPath expressions. Parsel is particularly useful for web scraping tasks where you need to programmatically extract specific d
    2 min read
  • How to remove HTML tags from data in PHP ?
    Removing HTML tags from data in PHP is a crucial step for sanitizing user input or displaying content safely. This process involves using the strip_tags() function to eliminate any HTML or PHP tags from a string, leaving only plain text. It's essential for preventing potential security risks, such a
    1 min read
  • How to Remove tags using BeautifulSoup in Python?
    Prerequisite- Beautifulsoup module In this article, we are going to draft a python script that removes a tag from the tree and then completely destroys it and its contents. For this, decompose() method is used which comes built into the module. Syntax: Beautifulsoup.Tag.decompose() Tag.decompose() r
    2 min read
  • How to Install Python Scrapy on Ubuntu?
    Scraping is the process of collection of web metadata or web information through web crawlers. We can get the links associated with the domain, can also retrieve the JavaScript file links, and many more. For performing web scraping we use the Scrapy library. It is purely written in Python. In this a
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences