w3resource
BeautifulSoup Exercises

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:

[ 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.]



New Content: Composer: Dependency manager for PHP, R Programming


bedava porno