Skip to main content

Section 14.7 Plan 9: Print info

Itโ€™s very common to print the information you have just scraped.

Subsection 14.7.1 Plan 9: Example

If you want to print info from only one tag, like if youโ€™ve just used Plan 4: Get info from a single tag, then do this:
Goal: Print the info # Print the info print(info)
If you want to print info from multiple tags, like if youโ€™ve just used Plan 5: Get info from all tags of a certain type, then do this:
Goal: Print the info # Print the info print(collect_info)

Subsection 14.7.2 Plan 9: Exercises

Activity 14.7.1.

Here is the code to collect all the locations from the Cottage Inn locations page.
What should fill the slot in Plan 9 below?
#Get one webpage
# Load libraries for web scraping
from bs4 import BeautifulSoup
import requests
# Get a soup from a URL
url = 'https://cottageinn.com/pick-a-location/'
r = requests.get(url)
soup = BeautifulSoup(r.content, 'html.parser')

#Get info from all tags of a certain type
# Get all tags of a certain type from the soup
tags = soup.find_all('h3')
# Collect info from the tags
collect_info = []
for tag in tags:
    # Get info from tag
    info = tag.text
    collect_info.append(info)

#Print the info
# Print the info
print(___________)
  • collect_info
  • Correct.
  • info
  • Remember that the variable that goes in this plan depends on the plan above.
You have attempted of activities on this page.