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.
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()))
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.
What is the difficulty level of this exercise?
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Getting the last element of a list:
some_list[-1] is the shortest and most Pythonic.
In fact, you can do much more with this syntax. The some_list[-n] syntax gets the nth-to-last element. So some_list[-1] gets the last element, some_list[-2] gets the second to last, etc, all the way down to some_list[-len(some_list)], which gives you the first element.
You can also set list elements in this way. For instance:
>>> some_list = [1, 2, 3] >>> some_list[-1] = 5 # Set the last element >>> some_list[-2] = 3 # Set the second to last element >>> some_list [1, 3, 5]
Note that getting a list item by index will raise an IndexError if the expected item doesn't exist. This means that some_list[-1] will raise an exception if some_list is empty, because an empty list can't have a last element.
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework