Preview

4 - For loops, beautiful tables, put it together

 1. I have watched all four video lessons on webscraping.

  FALSE

  TRUE

 2. I have managed to scrape a website's HTML contents and display it on my own flask generated web page.

  FALSE

  TRUE

 3. In the following code, what is happening on line 13?
from flask import Flask, render_template
app=Flask(__name__)
from bs4 import BeautifulSoup
import requests

#'https://www.worldometers.info/coronavirus
url='https://en.m.wikipedia.org/wiki/List_of_largest_Internet_companies'
req=requests.get(url)
bsObj=BeautifulSoup(req.text,"html.parser")

data=bsObj.find('table',{'class':'wikitable sortable mw-collapsible'})

table_data=[]
trs=bsObj.select('table tr')

  Line 13 is creating a variable called table_data that will store a div class

  Line 13 will result in an error. The brackets should be curly like so: table_data={}

  Line 13 is creating an empty class into which data will be scraped

  Line 13 is creating an empty list called table_data

 4. Following on from the above code, what is the tr in lines 2 and 4 referring to?
table_data=[]
trs=bsObj.select('table tr')

for tr in trs[1:6]: #first element is empty - 
	row=[]
	for t in tr.select('td')[:3]:
		row.extend([t.text.strip()])
	table_data.append(row)
data=table_data

@app.route('/')
def home():
	return render_template('home.html',data=data)

  rows

  table file contents

  table class

  columns

 5. In the following code, td on line 10 is referring to:
url = 'https://en.m.wikipedia.org/wiki/List_of_largest_Internet_companies' 
req = requests.get(url) 
bsObj = BeautifulSoup(req.text, 'html.parser')
data = bsObj.find('table',{'class':'wikitable sortable mw-collapsible'})

table_data=[]
trs = bsObj.select('table tr')
for tr in trs[1:6]: #first element is empty
    row = []
    for t in tr.select('td')[:3]:    #td is referring to ?
        row.extend([t.text.strip()])
    table_data.append(row)
data=table_data

rank=data[0][0]
name=data[0][1]
revenue=data[0][2]

  the columns

  the rows

  the whole file (table data file)

  the data in the table, and not the structure of the table

 6. In line 3, note the use of [:3]. Take a similar example where s='foobar'. What is the output of s[:4]?
for tr in trs[1:6]: #first element is empty - 
	row=[]
	for t in tr.select('td')[:3]:
		row.extend([t.text.strip()])
	table_data.append(row)
data=table_data

  foob

  barf

  b

  f

 7. Given the following code, what needs to go on line 4 in order to pass the contents of 'data' to the html page home.html?
data=table_data
@app.route('/')
def home():
	return render_template('home.html')

  Line 4 should replace the 'home.html' with 'data.html' to pass the contents of data to the page

  None of the listed items are correct as data cannot be directly passed to the HTML

  Line 4 should be:return render_template('home.html',data=data)

  Line 4 should be: return render_template('home.html',{{data}})

 8. Following on from the example in the video where the contents of data are being split into individual elements and displayed on the page, what jinja code should go on on line 1?
 {% for  %}
        
          #
          {{element[0]}}
          {{element[1]}}
          {{element[2]}}
        
        {% endfor %}

  Line 1 should be: {% for data[0],data[1],data[2] in data %}

  Line 1 should be: {% for element in data %}

  Line 1 should be: {% for data in element %}

  Line 1 should be: {% for data in data %}

 9. The following code uses the 'find_all' function to retrieve __________________________ in the HTML and store it in the 'all_tables' variable.
all_tables=soup.find_all("table")
all_tables

  a single instance of the 'table' tag

  all instances of the word "table" iin the html webpage being scraped

  all instances of the word 'table' in the HTML text

  all instances of the 'table' tag

 10. Looking at the following code, we can conclude that the class id of our chosen table is:
right_table=soup.find('table', class_='wikitable sortable')
right_table

  class_

  "wikitable sortable"

  table

  right_table