Problem Statment:
Write a Python program to find all the h2 tags and list the first four from the webpage python.org?
Solution:
1 2 3 4 5 6 7 | import requests from bs4 import BeautifulSoup url = 'https://www.python.org/' reqs = requests.get(url) soup = BeautifulSoup(reqs.text, 'lxml') print("First four h2 tags from the webpage python.org.:") print(soup.find_all('h2')[0:4]) |
Output
1 2 3 4 5 | First four h2 tags from the webpage python.org.: [<h2 class="widget-title"><span aria-hidden="true" class="icon-get-started"></span>Get Started</h2>, <h2 class="widget-title"><span aria-hidden="true" class="icon-download"></span>Download</h2>, <h2 class="widget-title"><span aria-hidden="true" class="icon-documentation"></span>Docs</h2>, <h2 class="widget-title"><span aria-hidden="true" class="icon-jobs"></span>Jobs</h2>] |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.