Python BeautifulSoup: Print content of elements that contain a specified string of a given web page
BeautifulSoup: Exercise-18 with Solution
Write a Python program to print content of elements that contain a specified string of a given web page.
Sample Solution:
Python Code:
import requests
import re
from bs4 import BeautifulSoup
url = 'https://www.python.org/'
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'lxml')
print("\nContent of elements that contain 'Python' string:")
str1 = soup.find_all(string=re.compile('Python'))
for txt in str1:
print(" ".join(txt.split()))
Sample Output:
Content of elements that contain 'Python' string: Welcome to Python.org Python The Python Network Python Brochure Python Books Python Essays Python Conferences Python Logo Python Wiki Python News Python Events Python Events Archive # Python 3: Fibonacci series up to n The core of extensible programming is defining functions. Python allows mandatory and optional arguments, keyword arguments, and even arbitrary argument lists. More about defining functions in Python 3 # Python 3: List comprehensions Lists (known as arrays in other languages) are one of the compound data types that Python understands. Lists can be indexed, sliced and manipulated with other built-in functions. More about lists in Python 3 # Python 3: Simple arithmetic Calculations are simple with Python, and expression syntax is straightforward: the operators More about simple math functions in Python 3 # Python 3: Simple output (with Unicode) >>> print("Hello, I'm Python!") Hello, I'm Python! What is your name? Python Hi, Python. Experienced programmers in any other language can pick up Python very quickly, and beginners find the clean syntax and indentation structure easy to learn. with our Python 3 overview. Python knows the usual control flow statements that other languages speak — More control flow tools in Python 3 Python is a programming language that lets you work quickly Whether you're new to programming or an experienced developer, it's easy to learn and use Python. Python source code and installers are available for download for all versions! Python 3.7.3 Documentation for Python's standard library, along with tutorials and guides, are available online. Looking for work or have a Python related position that you're trying to hire for? Our Python Core Developer Mentorship Scott Shawcroft: History of CircuitPython Python Toulouse : Jupyter: Présentation d’un écosystème 🔭 Python Meeting Düsseldorf "Python is all about automating repetitive tasks, leaving more time for your other SEO efforts." Using Python scripts to analyse SEO and broken links on your site Use Python for… wxPython IPython Python Enhancement Proposals : The future of Python Python Software Foundation The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. Python Brochure Python Books Python Essays Python Conferences Python Logo Python Wiki Python News Python Events Python Events Archive Python Software Foundation
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to find and print all li tags of a given web page.
Next: Write a Python program to print the element(s) that has a specified id of a given web page.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/BeautifulSoup/python-beautifulsoup-exercise-18.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics