Python BeautifulSoup Exercises, Practice, Solution
[An editor is available at the bottom of the page to write and execute the scripts.]
BeautifulSoup [36 exercises with solution]
1. Write a Python program to find the title tags from a given html document. Go to the editor
Click me to see the sample solution
2. Write a Python program to retrieve all the paragraph tags from a given html document. Go to the editor
Click me to see the sample solution
3. Write a Python program to get the number of paragraph tags of a given html document. Go to the editor
Click me to see the sample solution
4. Write a Python program to extract the text in the first paragraph tag of a given html document. Go to the editor
Click me to see the sample solution
5. Write a Python program to find the length of the text of the first <h2> tag of a given html document. Go to the editor
Click me to see the sample solution
6. Write a Python program to find the text of the first <a> tag of a given html text. Go to the editor
Click me to see the sample solution
7. Write a Python program to find the href of the first <a> tag of a given html document. Go to the editor
Click me to see the sample solution
8. Write a Python program to extract all the URLs from the webpage python.org that are nested within <li> tags from . Go to the editor
Click me to see the sample solution
9. Write a Python program to find all the h2 tags and list the first four from the webpage python.org. Go to the editor
Click me to see the sample solution
10. Write a Python program to find all the link tags and list the first ten from the webpage python.org. Go to the editor
Click me to see the sample solution
11. Write a Python program to a list of all the h1, h2, h3 tags from the webpage python.org. Go to the editor
Click me to see the sample solution
12. Write a Python program to extract all the text from a given web page. Go to the editor
Click me to see the sample solution
13. Write a Python program to print the names of all HTML tags of a given web page going through the document tree. Go to the editor
Click me to see the sample solution
14. Write a Python program to retrieve children of the html tag from a given web page. Go to the editor
Click me to see the sample solution
15. Write a Python program to retrieve all descendants of the body tag from a given web page. Go to the editor
Click me to see the sample solution
16. Write a Python program to retrieve the HTML code of the title, its text, and the HTML code of its parent. Go to the editor
Click me to see the sample solution
17. Write a Python program to find and print all li tags of a given web page. Go to the editor
Click me to see the sample solution
18. Write a Python program to print content of elements that contain a specified string of a given web page. Go to the editor
Click me to see the sample solution
19. Write a Python program to print the element(s) that has a specified id of a given web page. Go to the editor
Click me to see the sample solution
20. Write a Python program to create a Beautiful Soup parse tree into a nicely formatted Unicode string, with a separate line for each HTML/XML tag and string. Go to the editor
Click me to see the sample solution
21. Write a Python program to find the first tag with a given attribute value in an html document. Go to the editor
Click me to see the sample solution
22. Write a Python program to find tag(s) beneath other tag(s) in a given html document. Go to the editor
Click me to see the sample solution
23. Write a Python program to find tag(s) directly beneath other tag(s) in a given html document. Go to the editor
Click me to see the sample solution
24. Write a Python program to find the siblings of tags in a given html document. Go to the editor
Click me to see the sample solution
25. Write a Python program to find tags by CSS class in a given html document. Go to the editor
Click me to see the sample solution
26. Write a Python program to change the tag's contents and replace with the given string. Go to the editor
Click me to see the sample solution
27. Write a Python program to add to a tag's contents in a given html document. Go to the editor
Click me to see the sample solution
28. Write a Python program to insert a new text within a url in a specified position. Go to the editor
Click me to see the sample solution
29. Write a Python program to insert tags or strings immediately before specified tags or strings. Go to the editor
Click me to see the sample solution
30. Write a Python program to insert tags or strings immediately after specified tags or strings. Go to the editor
Click me to see the sample solution
31. Write a Python program to remove the contents of a tag in a given html document. Go to the editor
Click me to see the sample solution
32. Write a Python program to extract a tag or string from a given tree of html document. Go to the editor
Click me to see the sample solution
33. Write a Python program to remove a tag from a given tree of html document and destroy it and its contents. Go to the editor
Click me to see the sample solution
34. Write a Python program to remove a tag or string from a given tree of html document and replace it with the given tag or string. Go to the editor
Click me to see the sample solution
35. Write a Python program to wrap an element in the specified tag and create the new wrapper. Go to the editor
Click me to see the sample solution
36. Write a Python program to replace a given tag with whatever's inside a given tag. Go to the editor
Click me to see the sample solution
Python Code Editor:
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.
[ Want to contribute to Python Pandas exercises? Send your code (attached with a .zip file) to us at w3resource[at]yahoo[dot]com. Please avoid copyrighted materials.]
Test your Python skills with w3resource's quiz
Python: Tips of the Day
Find current directory and file's directory:
To get the full path to the directory a Python file is contained in, write this in that file:
import os dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)
To get the current working directory use
import os cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The os and os.path modules.
- The __file__ constant
- os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
- os.path.dirname(path) (returns "the directory name of pathname path")
- os.getcwd() (returns "a string representing the current working directory")
- os.chdir(path) ("change the current working directory to path")
Ref: https://bit.ly/3fy0R6m
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- 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
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework