D
D
driveknight2021-11-10 02:12:11
Python
driveknight, 2021-11-10 02:12:11

How to parse elements using BeautifulSoup?

Hello. Here is the part of my parser that gets the prices:

def get_soup(lp: list):
    for i in lp:
        with open('html.txt', 'w', encoding='utf-8') as f:
            response = requests.get(i, headers=headers, cookies=cookies)
            f.write(response.text)
            with open("html.txt", encoding='utf-8') as nf:
                soup = BeautifulSoup(nf, 'html.parser')
                tags = soup.find(lambda tag: tag.name == 'div' and tag.get('class') == ['n_6'])

                for prices in tags.find_all('span', class_='N_1'):
                    price_re = re.sub(r'\D', '', *prices)
                    price.append(price_re)
                
                for p_prices in tags.find_all('p', class_='N_'):
                    p_price_re = re.sub(r'\D', '', *p_prices)
                    p_price.append(p_price_re)

From here:
<div class="NZ N_2"><p class="N_">3 149&nbsp;₽</p><p class="N_0"><span class="N_1">4 999&nbsp;₽</span></p></div>


The problem is that when products come across without a promotional price, then all prices from this product begin to be recorded incorrectly. Promotional prices if that are recorded correctly. I kind of understand why this error, but I can’t process it or write a check. With operators inand isit doesn't work because the whole page is parsed. Is parsing each product separately the only way out? I would like to implement this with BeautifulSoup

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2021-11-10
@phaggi

I suppose, instead of writing prices in a list, it is better to organize a dictionary in which you write normal prices under numbered keys, and write a promo price in the “promo” key.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question