BeautifulSoup: Problems with numbers

Viewed 42

I am getting some trouble with my code. The thing is that I'm extracting prices for a website and some of this prices includes the original price and the discounted one, and what i'm trying to get is only the discounted one. How do i fix it?

import requests
from bs4 import BeautifulSoup

search_url = "https://store.steampowered.com/search/?sort_by=Price_ASC&category1=998%2C996&category2=29"
category1 = ('998', '996')
category2 = '29'

params = {
    'sort_by': 'Price_ASC',
    'category1': ','.join(category1),
    'category2': category2,
}

response = requests.get(
    search_url,
    params=params
)

soup = BeautifulSoup(response.text, "html.parser")
elms = soup.find_all("span", {"class": "title"})
prcs = soup.find_all("div",{"class": "col search_price discounted responsive_secondrow"})
for elm in elms: 
    print(elm.text)
for prc in prcs:
    print(prc.text)
2 Answers

Use next_sibling

prcs = soup.find_all("div",{"class": "col search_price discounted responsive_secondrow"})

for p in prcs:
    print(p.find('span').next_sibling.next)

You can also use .contents. .contents list all the children of the selected tag and from there select your desired tag, in your case the last children of the tag.

for prc in prcs:
    print(prc.contents[-1])
Related