14.7. Plan 9: Print info¶
It’s very common to print the information you have just scraped.
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)
14.7.2. Plan 9: Exercises¶
csp-10-2-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(___________)
___________ should be
You have attempted of activities on this page