w3resource

Python BeautifulSoup Exercises, Practice, Solution

[An editor is available at the bottom of the page to write and execute the scripts. Go to the editor]

BeautifulSoup [36 exercises with solution]

1. Write a Python program to find the title tags from a given html document.
Click me to see the sample solution

2. Write a Python program to retrieve all the paragraph tags from a given html document.
Click me to see the sample solution

3. Write a Python program to get the number of paragraph tags of a given html document.
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.
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.
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.
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.
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 .
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.
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.
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.
Click me to see the sample solution

12. Write a Python program to extract all the text from a given web page.
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.
Click me to see the sample solution

14. Write a Python program to retrieve children of the html tag from a given web page.
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.
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.
Click me to see the sample solution

17. Write a Python program to find and print all li tags of a given web page.
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.
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.
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.
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.
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.
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.
Click me to see the sample solution

24. Write a Python program to find the siblings of tags in a given html document.
Click me to see the sample solution

25. Write a Python program to find tags by CSS class in a given html document.
Click me to see the sample solution

26. Write a Python program to change the tag's contents and replace with the given string.
Click me to see the sample solution

27. Write a Python program to add to a tag's contents in a given html document.
Click me to see the sample solution

28. Write a Python program to insert a new text within a url in a specified position.
Click me to see the sample solution

29. Write a Python program to insert tags or strings immediately before specified tags or strings.
Click me to see the sample solution

30. Write a Python program to insert tags or strings immediately after specified tags or strings.
Click me to see the sample solution

31. Write a Python program to remove the contents of a tag in a given html document.
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.
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.
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.
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.
Click me to see the sample solution

36. Write a Python program to replace a given tag with whatever's inside a given tag.
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



Follow us on Facebook and Twitter for latest update.