UoM Online Course 3 - Python Programming එක ඉවර කරපු අය...

SL MULTIMEDIA TUTORIAL

Well-known member
  • Jul 27, 2020
    1,639
    6,950
    113
    United States
    UoM Online Course 3 - Python Programming ඉවර කරපු අය...

    course එක ඉවර කරපු කෙනෙක් ඉන්නවානම් කියන්න පුලුවන්ද Programming Assessment එකේ Code එක එන විදිහ Try කරලා බැලුවා .:sorry:🥲 කරගන්න බැරි නිසා help එකක් ඉල්ලලා බලන්න හිතුනේ..:rolleyes:

    Programming Assessment එක​


    There are two online supermarkets which provide price of various products.

    You need to write a program to acquire, extract and compare the price from these two suppliers, for a given product.

    Example: Price of a coconut from the two different suppliers can be found in the following webpages.

    laughs_coconut = 'https://www.laugfssuper.com/index.php/coconut-105320.html' (Please check the update note at the end of the question)
    glomark_coconut = 'https://glomark.lk/coconut/p/11624'

    The function def compare_prices(product_laughs,product_glomark) will take-in two similar products from two suppliers, and compare the prices to recommend you which option is cheaper on a given instance of time.

    1. Visit the links in your web browser, and use the Inspect elements / View page source tools of the browser to understand the structure of the web pages given above.
    2. Complete the compare_prices function which is pre-loaded in the answer box to acquire and extract the price values from two web pages.



    When you submit your answer to be checked, be patient for a while (up to 20 seconds) until its been checked with multiple test cases.
    Example expected output

    Laughs COCONUT - Item#mr-2058 Rs.: 89.0
    Glomark Coconut Rs.: 86.0
    Glomark is cheaper: 3.0

    Here are some more test cases for you to test in your own computer.

    laughs_tissues = 'https://www.laugfssuper.com/index.php/categories/household/flora-facial-tissues-2-x-160-box.html'
    glomark_tissues = 'https://glomark.lk/flora-facial-tissues-160s/p/10470'
    compare_prices(laughs_tissues,glomark_tissues)

    laughs_bread = https://www.laugfssuper.com/index.php/crimson-bread-sliced-111452.html'
    glomark_bread = 'https://glomark.lk/top-crust-bread/p/13676'
    compare_prices(laughs_bread,glomark_bread)

    **Update:Laugfs website seems to be slow and changing quite frequently. So a backup of the required web pages are available in the following links. Your answers will be validated against these pages.:

    https://scrape-sm1.github.io/site1/COCONUT market1super.html
    https://scrape-sm1.github.io/site1/... HOUSEHOLD - Categories market1super.com.html
    https://scrape-sm1.github.io/site1/...BEVERAGES - Categories market1ssuper.com.html
    https://scrape-sm1.github.io/site1/Crimson Bread Sliced market1super.com.html

    Stock Code eka :nerd:


    Python:
    import requests
    import json
    
    import sys
    sys.path.insert(0,'bs4.zip')
    from bs4 import BeautifulSoup
    
    #Imitate the Mozilla browser.
    user_agent = {'User-agent': 'Mozilla/5.0'}
    
    
    def compare_prices(product_laughs,product_glomark):
        #TODO: Aquire the web pages which contain product Price
    
        
        
        #TODO: LaughsSuper supermarket website provides the price in a span text.
    
    
        #TODO: Glomark supermarket website provides the data in jason format in an inline script.
        #You can use the json module to extract only the price
        
        
        #TODO: Parse the values as floats, and print them.
        
        print('Laughs  ',product_name_laughs,'Rs.: ' , price_laughs)
        print('Glomark ',product_name_glomark,'Rs.: ' , price_glomark)
        
        if(price_laughs>price_glomark):
            print('Glomark is cheaper Rs.:',price_laughs - price_glomark)
        elif(price_laughs<price_glomark):
            print('Laughs is cheaper Rs.:',price_glomark - price_laughs)   
        else:
            print('Price is the same')
     

    pasindu201

    Well-known member
  • Dec 25, 2017
    1,737
    1,523
    113
    inspect element gihilla oyata awashya element eke thiyena thana hoyaganna,eetapasse beautiful soup eken a place ekata gihin data eka ganna,eka variable ekaka save karaganna,a widihatama anith website ekatath karanna,
     

    pasindu201

    Well-known member
  • Dec 25, 2017
    1,737
    1,523
    113
    mamath godak danne na,mage code ekanam pass una,onenam dennam,ehema wadk nane igenaganne nathuwa,,prashna danna,danna deyak kiyala dennam,danne nathi deyak hoyala kiyannam
     

    SL MULTIMEDIA TUTORIAL

    Well-known member
  • Jul 27, 2020
    1,639
    6,950
    113
    United States
    mamath godak danne na,mage code ekanam pass una,onenam dennam,ehema wadk nane igenaganne nathuwa,,prashna danna,danna deyak kiyala dennam,danne nathi deyak hoyala kiyannam

    menna mage Code eka meke Error eka mokakda 🥲 :sorry:

    Python:
    import requests
    import json
    
    import sys
    sys.path.insert(0,'bs4.zip')
    from bs4 import BeautifulSoup
    
    #Imitate the Mozilla
    user_agent = {'User-agent': 'Mozilla/5.0'}
    
    
    
    def compare_prices(product_laughs,product_glomark):
    
    
        # laugfs product
        html_laugfs = requests.get(product_laughs).content
        soup_laugfs = BeautifulSoup(html_laugfs, 'html.parser')
    
        prod_laugfs = soup_laugfs.find('span',class_="regular-price")
    
        prodName = soup_laugfs.find('div',class_="product-name")
        product_name_laughs=prodName.get_text()
    
        prod_laugfs = prod_laugfs.get_text() # Will get text from html tags
        product_laugfs = prod_laugfs.strip() # Removing special characters like \n (newline)
        #print(product_name_laughs)
        prod_price_laugfs = product_laugfs[3:]
        price_laughs = float(prod_price_laugfs)
        #print(price_laughs)
    
        # Glomark product
    
        html_glomark = requests.get(product_glomark).content
        soup_glomark = BeautifulSoup(html_glomark, 'html.parser')
    
        prod_glomark = soup_glomark.find('script',type="application/ld+json")
        prod_glomark = prod_glomark.get_text()
        prod_glomark = json.loads(prod_glomark)
    
        product_name_glomark = prod_glomark['description']
        offers_glomark = prod_glomark['offers'][0]
        price_glomark = float(offers_glomark['price'])
        #print(product_name_glomark)
        #print(price_glomark)
    
       
        print('Laughs  ',product_name_laughs,'Rs.: ' , price_laughs)
        print('Glomark ',product_name_glomark,'Rs.: ' , price_glomark)
       
        if(price_laughs>price_glomark):
            print('Glomark is cheaper Rs.:',price_laughs - price_glomark)
        elif(price_laughs<price_glomark):
            print('Laughs is cheaper Rs.:',price_glomark - price_laughs)  
        else:
            print('Price is the same')
    
    
    laughs_coconut = 'https://scrape-sm1.github.io/site1/COCONUT%20market1super.html'
    glomark_coconut = 'https://glomark.lk/coconut/p/11624'
    
    laughs_tissues = 'https://www.laugfssuper.com/index.php/categories/household/flora-facial-tissues-2-x-160-box.html'
    glomark_tissues = 'https://glomark.lk/flora-facial-tissues-160s/p/10470'
    
    laughs_bread = 'https://www.laugfssuper.com/index.php/crimson-bread-sliced-111452.html'
    glomark_bread = 'https://glomark.lk/top-crust-bread/p/13676'
    
    #compare_prices(laughs_coconut,glomark_coconut)
    
    compare_prices(laughs_bread,glomark_bread)
     
    Last edited:

    pasindu201

    Well-known member
  • Dec 25, 2017
    1,737
    1,523
    113
    keeweni script ekada kiyala uda idan ganan karala a script eka witharak load karanam hari neda?

    #inline script contains the informaation,check source code
    findInlineScript= glowSoup.find_all('script')[6].text
    jsonObjectfromScript = json.loads(findInlineScript)



    price_glomark = float(jsonObjectfromScript["offers"][0]["price"])
     

    SL MULTIMEDIA TUTORIAL

    Well-known member
  • Jul 27, 2020
    1,639
    6,950
    113
    United States
    keeweni script ekada kiyala uda idan ganan karala a script eka witharak load karanam hari neda?

    #inline script contains the informaation,check source code
    findInlineScript= glowSoup.find_all('script')[6].text
    jsonObjectfromScript = json.loads(findInlineScript)



    price_glomark = float(jsonObjectfromScript["offers"][0]["price"])
    mage Code eke waradda mokakda kiyanna Puluwanda
     

    pasindu201

    Well-known member
  • Dec 25, 2017
    1,737
    1,523
    113
    from traceback import print_list
    import requests
    import json

    import sys
    sys.path.insert(0,'bs4.zip')
    from bs4 import BeautifulSoup

    #Imitate the Mozilla
    user_agent = {'User-agent': 'Mozilla/5.0'}



    def compare_prices(product_laughs,product_glomark):


    # laugfs product
    html_laugfs = requests.get(product_laughs).content
    soup_laugfs = BeautifulSoup(html_laugfs, 'html.parser')

    prod_laugfs = soup_laugfs.find('span',class_="regular-price")

    prodName = soup_laugfs.find('div',class_="product-name")
    product_name_laughs=prodName.get_text()

    prod_laugfs = prod_laugfs.get_text() # Will get text from html tags
    product_laugfs = prod_laugfs.strip() # Removing special characters like \n (newline)
    #print(product_name_laughs)
    prod_price_laugfs = product_laugfs[3:]
    price_laughs = float(prod_price_laugfs)
    #print(price_laughs)

    # Glomark product

    html_glomark = requests.get(product_glomark).content
    soup_glomark = BeautifulSoup(html_glomark, 'html.parser')

    prod_glomark = soup_glomark.find_all('script')[6].text
    prod_glomark = json.loads(prod_glomark)

    product_name_glomark = prod_glomark['description']
    offers_glomark = prod_glomark['offers'][0]
    price_glomark = float(offers_glomark['price'])
    #print(product_name_glomark)
    #print(price_glomark)


    print('Laughs ',product_name_laughs,'Rs.: ' , price_laughs)
    print('Glomark ',product_name_glomark,'Rs.: ' , price_glomark)

    if(price_laughs>price_glomark):
    print('Glomark is cheaper Rs.:',price_laughs - price_glomark)
    elif(price_laughs<price_glomark):
    print('Laughs is cheaper Rs.:',price_glomark - price_laughs)
    else:
    print('Price is the same')

    me widihata wenas karanna one,hariyatama script eke number eka one,anika kalin oya manual function ekata call karala thibba ethakota deparak output eka print wenwa,ehema unama eyala dena test cases walata hariyanne na,mkada eyalage test case eke ekai oyage ekai dekak print wenwa
    ------ Post added on Apr 26, 2022 at 12:43 AM