Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautiful Soup Finding if a tag exists

I've been trying to figure this code out with using Beautiful Soup. What I want it to do since it I am pulling various pages and they don't have consistent tags, if it finds one tag to run that code and if it finds another tag to run another code, but I've had no success so far, I'd appreciate some help:

I am not getting any error messages, I do not know if the loop breaks or if it just skips it but it's not printing anything.

The URLs are:

  1. http://store.steampowered.com/app/271590/Grand_Theft_Auto_V/

  2. http://store.steampowered.com/app/578080/PLAYERUNKNOWNS_BATTLEGROUNDS/

The code I have is:

for price in pricing:

        if pricing.find('discount_final_price'):
            game_price = price.find('discount_final_price')
            gamprice = game_price[i].text
            print("Price:" + gamprice)  
        else :
            game_price = price.find("game_purchase_price price")
            gamprice = game_price[i].text
            print("Price:" + gamprice)
like image 332
IoH Avatar asked Jan 29 '23 01:01

IoH


1 Answers

hi I dont know what is in the price variable but this code work well :

for price in pricing:

    game_price = soup.find('div', class_='discount_final_price')

    if game_price != None :
        print( "Price : {}".format(game_price.text) )

    else :
        game_price = pricing.find('div', class_='game_purchase_price price')
        print( "Price : {}".format(game_price.text) )
like image 160
eden clarck Avatar answered Jan 31 '23 21:01

eden clarck