Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

python 3 questions + Code + Text Question no. 14: In this section you are going to use some advance web-scrapping ideas. However you don't

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
python 3 questions
+ Code + Text Question no. 14: In this section you are going to use some advance web-scrapping ideas. However you don't have to code anything for that. But you have to post-process the content you get from the internet. This processing includes mapping - filtering and mapping task. This exercise is followed by a given code snippet which rewards you for your hard work. 5x3= 15 points) research (1 I hope you will learn something new here TE you feel anything confusing please do # I am not going through all the details here Import 3 from bat import Beautiful Soup LIPO 3 Laport random # Please do not feel confused with following 4 lines of code # These codes o grab all the html content from website # And we get all the image tag in a list called tags in line 11 http- ur . Pool Manager - http.reques (CET. https boredana com funniestelinee soup - Rautiful soupidata) tagsp.tidalling" t g ooglebut medic emples #Lets see how these tags look like we are looking only 10 random sample(tags, 10) leta grab first item of these tags and see wype # You will find it is nos " E' but something else type (bags (0) ing with the following lambida operation # Now you have to map these tage to s COSE - Landa tage- p(751. Complete the code After we mapped everything to string we still need to get speeitse tags Use following laws operation to filter getoke-lambda funniest-to-i -jokes tags = ter(231 [] Now we need only the link to the joke not a full tag # use following map to map the tag inte link filterer - Lamoda NIK. partition ( SEO )-1) partition tag a p() Now there is the fun part You don't have to do anything here . If you you verything right . You can run this coll many times you like and read wonderful Jokes Dili These jokes are grabbed tre interes take from PIL pors Tage port requests from toport Bytesto Endomsad() - Dictionary 1 Dictionary is an interesting data structu # It holds key value pairs # Values can be accessed by using keys # Lets see few of the ways to create a dictionary a = {'a': 'b':2, print('a : {a}') 'e':3, 'd' :4) b- diet (zip (['a', 'b', 'c' print ( b: {b}') (1,2,3, c = dict(iter(l'a, 1), ('b', 2), t'e',3), ('a', print('c : {c}"? d - dict(a=1, b=2, -3, d-4) 2. -3. d-4) print it'd : (d)) + Accessing values using keys in dictionary print (d'altgetting values corresponding to key # Study the following code closely + Here is a function that returns a value # given a key and a dictionary as arguments # For now you don't have to know the details of # how to define a function # A very simple function def get from (dictionary, keys): values - dictionary keya) return values 11 . Now lets use this function to get values corresponding + trom dictionary "d values - get from (dictionary, 'd', keys='a') print 'values correspoding to a 1s (values)) return values TypeError Traceback (most recent call last) in ) -> 2 values - get from dictionary 'd', keys 'a') 3 print('values correspoding to a is values)) 4 return values in () 2 values - get from(dietionary, 'd', keys='a') 3 print (f values correspoding to a is {values}") 4 return values in getfrom (dictionary, keya) 1 def get from(dietionary, keys): ----> 2 values = dictionary keys] 3 return values TypeError: String indices must be int SEARCH STACK OVERFLOW [] # Now lets try to access '' which is not in 'd' values - get from (dietionaryd, keys - ) print('values COELONpoding to a values Name Error Traceback (most recent call last) in 0) --> 2 values - get from(dictionaryd, keys - Tz 3 print if'values Correspoding to is values") NameError: name 'd' is not defined SEARCH STACK OVERFLOW U So now you see we have an error, called a key error Like list dictionary has different methods Laplemented # like 'clear', 'copy', 'fromkeya', and so on + You now know how to use these methods + you can use one of these two methods to accomplish following task Question no. 6: Complete the function, so that if you don't have a key return 999. (10 points [ 1 Code goes here def get from dictionary, keys): values - 7 complete your code here return values should return 999 if key doesn't exist print (getfrom (dictionary=d, keys='z')) Question no. 7: Complete the function, so that if you don't have a key return 999 and add 999 to dictionary? [10 points 11 # Code goes here det get from (dictionary.keys): Name Traceback (most recent call last) in () ---> 2 values - get from (dictionaryd, keys - '2') 3 print (f' values correspoding to z is (values}') NameError: name 'd' is not defined SEARCH STACK OVERFLOW U + So now you see we have an error, called a key error Like list dictionary has different methods implemented + like 'elear', 'copy', 'fromkeys', .... and so on # You now know how to use these methods # you can use one of these two methods to accomplish following task Question no. 6: Complete the function, so that if you don't have a key return 999. (10 points) u Code goes here def getfrom(dictionary, keys): values = ? #complete your code here return values should return 999 if key print (getfrom(dictionaryd, keys-'*') Question no. 7: Complete the function, so that if you don't have a key return 999 and add 999 to dictionary? (10 points) U # Code goes here def get from(dictionary, keys): values = ? tcomplete your code here return values should return 999 if key doesn't exist and should add to dictionary as well print (getfrom(dictionary=d, keys='2')) + Now this shouldn't throw error as 'a' is now set to dictionary print (dt'z'])

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

7. How might you go about testing these assumptions?

Answered: 1 week ago