Our Services

Get 15% Discount on your First Order

[rank_math_breadcrumb]

from bs4 import BeautifulSoup import re import os import csv import unittest # IMPORTANT NOTE: “”” If you are getting “encoding errors” while trying to open, read, or write from a file, add the foll

from bs4 import BeautifulSoup

import re

import os

import csv

import unittest

# IMPORTANT NOTE:

“””

If you are getting “encoding errors” while trying to open, read, or write from a file, add the following argument to any of your open() functions:

   encoding=”utf-8-sig”

An example of that within the function would be:

   open(“filename”, “r”, encoding=”utf-8-sig”)

There are a few special characters present from Airbnb that aren’t defined in standard UTF-8 (which is what Python runs by default). This is beyond the scope of what you have learned so far in this class, so we have provided this for you just in case it happens to you. Good luck!

“””

def load_listing_results(html_file):

   “””

   INPUT: A string containing the path of the html file

   RETURN: A list of tuples

   “””

   with open(html_file, ‘r’, encoding = ‘utf-8-sig’) as f:

       soup = BeautifulSoup(f, ‘html.parser’)

   listings = []

   for link in soup.find_all(‘a’, href = True):

       if ‘rooms’ in link[‘href’]:

           title = link.text.strip()

           listing_id = link[‘href’].split(“https://studydaddy.com/”)[-1]

           listings.append((title, listing_id))

   return listings

def get_listing_details(listing_id):

   “””

   INPUT: A string containing the listing id

   RETURN: A tuple

   “””

   pass

def create_listing_database(html_file):

   “””

   INPUT: A string containing the path of the html file

   RETURN: A list of tuples

   “””

   pass

def output_csv(data, filename):

   “””

   INPUT: A list of tuples and a string containing the filename

   RETURN: None

   “””

   pass

def validate_policy_numbers(data):

   “””

   INPUT: A list of tuples

   RETURN: A list of tuples

   “””

   pass

# EXTRA CREDIT

def google_scholar_searcher(query):

   “””

   INPUT: query (str)

   Return: a list of titles on the first page (list)

   * see PDF instructions for more details

   “””

   pass

# TODO: Don’t forget to write your test cases!

class TestCases(unittest.TestCase):

   def setUp(self):

       self.listings = load_listing_results(“html_files/search_results.html”)

   def test_load_listing_results(self):

       # check that the number of listings extracted is correct (18 listings)

       self.assertEqual(len(self.listings), 18)

       # check that the variable you saved after calling the function is a list

       self.assertEqual(type(self.listings), list)

       # check that each item in the list is a tuple

       # check that the first title and listing id tuple is correct (open the search results html and find it)

       # check that the last title and listing id tuple is correct (open the search results html and find it)

   def test_get_listing_details(self):

       html_list = [“467507”,

                    “1550913”,

                    “1944564”,

                    “4614763”,

                    “6092596”]

       # call get_listing_details for i in html_list:

       listing_information = [get_listing_details(id) for id in html_list]

       # check that the number of listing information is correct

       self.assertEqual(len(listing_information), 5)

       for info in listing_information:

           # check that each item in the list is a tuple

           self.assertEqual(type(info), tuple)

           # check that each tuple has 6 elements

           self.assertEqual(len(info), 6)

           # check that the first four elements in the tuple are strings

           self.assertEqual(type(info[0]), str)

           self.assertEqual(type(info[1]), str)

           self.assertEqual(type(info[2]), str)

           self.assertEqual(type(info[3]), str)

           # check that the rest two elements in the tuple are integers

           self.assertEqual(type(info[4]), int)

           self.assertEqual(type(info[5]), int)

       # check that the first listing in the html_list has the correct policy number

       # check that the last listing in the html_list has the correct place type

       # check that the third listing has the correct cost

   def test_create_listing_database(self):

       detailed_data = create_listing_database(“html_files/search_results.html”)

       # check that we have the right number of listings (18)

       self.assertEqual(len(detailed_data), 18)

       for item in detailed_data:

           # assert each item in the list of listings is a tuple

           self.assertEqual(type(item), tuple)

           # check that each tuple has a length of 8

       # check that the first tuple is made up of the following:

       # (‘Loft in Mission District’, ‘1944564’, ‘2022-004088STR’, ‘Superhost’, ‘Brian’, ‘Entire Room’, 422, 181)

       # check that the last tuple is made up of the following:

       # (‘Guest suite in Mission District’, ‘467507’, ‘STR-0005349’, ‘Superhost’, ‘Jennifer’, ‘Entire Room’, 324, 165)

   def test_output_csv(self):

       # call create_listing_database on “html_files/search_results.html”

       # and save the result to a variable

       detailed_data = create_listing_database(“html_files/search_results.html”)

       # call output_csv() on the variable you saved

       output_csv(detailed_data, “test.csv”)

       # read in the csv that you wrote

       csv_lines = []

       with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), ‘test.csv’), ‘r’) as f:

           csv_reader = csv.reader(f)

           for i in csv_reader:

               csv_lines.append(i)

       # check that there are 19 lines in the csv

       self.assertEqual(len(csv_lines), 19)

       # check that the header row is correct

       # check that the next row is the correct information about Guest suite in San Francisco

       # check that the row after the above row is the correct infomration about Private room in Mission District

   def test_validate_policy_numbers(self):

       # call create_listing_database on “html_files/search_results.html”

       # and save the result to a variable

       detailed_data = create_listing_database(“html_files/search_results.html”)

       # call validate_policy_numbers on the variable created above and save the result as a variable

       invalid_listings = validate_policy_numbers(detailed_data)

       # check that the return value is a list

       self.assertEqual(type(invalid_listings), list)

       # check that the elements in the list are tuples

       # and that there are exactly three element in each tuple

def main ():

   detailed_data = create_listing_database(“html_files/search_results.html”)

   output_csv(detailed_data, “airbnb_dataset.csv”)

if __name__ == ‘__main__’:

   # main()

   unittest.main(verbosity=2)

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

DFA Closure: 1.4 c and f. Do union and intersection for each (use cross product algorithm) Each of the following languages is the intersection of two simpler languages. In each part, construct DFAs fo

DFA Closure: 1.4 c and f. Do union and intersection for each (use cross product algorithm) Each of the following languages is the intersection of two simpler languages. In each part, construct DFAs for the simpler languages, then combine them using the construction discussed in footnote 3 (page 46) to

DFA Closure: 1.4 c and f. Do union and intersection for each (use cross product algorithm) Each of the following languages is the intersection of two simpler languages. In each part, construct DFAs fo

DFA Closure: 1.4 c and f. Do union and intersection for each (use cross product algorithm) Each of the following languages is the intersection of two simpler languages. In each part, construct DFAs for the simpler languages, then combine them using the construction discussed in footnote 3 (page 46) to

1.2 Lab – More Charts in Excel Submission: Completed Excel workbook via Canvas. Part 1 – Multiple Variable Charts Overview • This problem furthers the ideas from the notes ‘1.2 – Basic Charts in Excel

1.2 Lab – More Charts in ExcelSubmission: Completed Excel workbook via Canvas.Part 1 – Multiple Variable ChartsOverview• This problem furthers the ideas from the notes ‘1.2 – Basic Charts in Excel’, where we learnedhow to create and format many simple charts in Excel.• The first tab of the accompanying workbook

TNM1 — Task 2: Multipage Website Prototype User Interface Design — C773 PRFA — TNM1 PreparationTask OverviewSubmissionsEvaluation ReportCompetencies 4040.01.1 : User Interface Design Projects The grad

TNM1 — Task 2: Multipage Website Prototype User Interface Design — C773 PRFA — TNM1 PreparationTask OverviewSubmissionsEvaluation ReportCompetencies 4040.01.1 : User Interface Design Projects The grad TNM1 — Task 2: Multipage Website Prototype User Interface Design — C773 PRFA — TNM1 PreparationTask OverviewSubmissionsEvaluation ReportCompetencies 4040.01.1 : User Interface Design Projects The graduate

In Units I, II, and III, you learned about the history of computers, application and system software, blockchain, cryptocurrency, computer ethics, and explored two Microsoft Office applications, Word

In Units I, II, and III, you learned about the history of computers, application and system software, blockchain, cryptocurrency, computer ethics, and explored two Microsoft Office applications, Word and Excel.  In this assignment you will demonstrate what you have learned in these three units. This assignment will have five parts. 

TNM1 — Task 2: Multipage Website Prototype User Interface Design — C773 PRFA — TNM1 PreparationTask OverviewSubmissionsEvaluation ReportCompetencies 4040.01.1 : User Interface Design Projects The grad

TNM1 — Task 2: Multipage Website Prototype User Interface Design — C773 PRFA — TNM1 PreparationTask OverviewSubmissionsEvaluation ReportCompetencies 4040.01.1 : User Interface Design Projects The graduate describes user interface design project constructs. 4040.01.2 : User Interface Design Process The graduate describes the user interface design process. 4040.01.3 : User Centered Web Design The graduate explains the

TNM1 — Task 1: Project Proposal with Strategies User Interface Design — C773 PRFA — TNM1 PreparationTask OverviewSubmissionsEvaluation ReportCompetencies 4040.01.1 : User Interface Design Projects The

TNM1 — Task 1: Project Proposal with Strategies User Interface Design — C773 PRFA — TNM1 PreparationTask OverviewSubmissionsEvaluation ReportCompetencies 4040.01.1 : User Interface Design Projects The graduate describes user interface design project constructs. 4040.01.2 : User Interface Design Process The graduate describes the user interface design process. 4040.01.3 : User Centered Web Design The graduate explains

TNM1 — Task 2: Multipage Website Prototype Competencies 4040.01.1 : User Interface Design Projects The graduate describes user interface design project constructs. 4040.01.2 : User Interface Design Pr

TNM1 — Task 2: Multipage Website Prototype Competencies 4040.01.1 : User Interface Design Projects The graduate describes user interface design project constructs. 4040.01.2 : User Interface Design Process The graduate describes the user interface design process. 4040.01.3 : User Centered Web Design The graduate explains the relationship between the user and the site design. 4040.01.4 : User Interface

IntroductionUser interface and user experience (UI/UX) designer is one of the most popular job titles in the technology industry. UI/UX designers tend to enjoy the challenges associated with creating

IntroductionUser interface and user experience (UI/UX) designer is one of the most popular job titles in the technology industry. UI/UX designers tend to enjoy the challenges associated with creating products that people love. Industry leaders know that design is a substantial competitive advantage, and they are competing for best talents;

In Units V, VI, and VII, you learned about the components of a computer, how a computer works, the internet, networks and network communications, cloud computing, web development, digital identity, so

In Units V, VI, and VII, you learned about the components of a computer, how a computer works, the internet, networks and network communications, cloud computing, web development, digital identity, social media, e-commerce, ethical behavior, databases, and explored two Microsoft Office applications, PowerPoint and Access.  In this assignment, you will

In the previous assignment, the annotated bibliography, you collected 15 – 20 references. Now, you are to craft an analytical research paper. Using the same themes assigned for the previous assignment

In the previous assignment, the annotated bibliography, you collected 15 – 20 references. Now, you are to craft an analytical research paper. Using the same themes assigned for the previous assignment.  GROUP 1: Context-Aware Computing  Prepare a ten (10) page research paper. In the paper, you are to cover the

This week, you will submit the second project, the Desktop Migration Proposal. Using the requirements analysis your manager provided and the Internet research you conducted, submit your recommendation

This week, you will submit the second project, the Desktop Migration Proposal. Using the requirements analysis your manager provided and the Internet research you conducted, submit your recommendation to the assignment folder. As you are writing your recommendation, ensure your analysis and recommendations align with your manager’s priorities and concerns.

In Units I, II, and III, you learned about the history of computers, application and system software, blockchain, cryptocurrency, computer ethics, and explored two Microsoft Office applications, Word

In Units I, II, and III, you learned about the history of computers, application and system software, blockchain, cryptocurrency, computer ethics, and explored two Microsoft Office applications, Word and Excel.  In this assignment you will demonstrate what you have learned in these three units. This assignment will have five parts.