Search code examples
pythonjsonloopsbeautifulsouphtml-parsing

How to get Previous Element with BeautifulSoup


I've been stuck on this for a while using BeautifulSoup html parser to write on db. I just have one price and i want to use that price with high and close price in future loops squence. Can somebody please help? I'll be forever grateful! I'm trying to store some data that's scraped from a website; Here are my python codes :

    soup = BeautifulSoup(html, 'html.parser')
    site_json=json.loads(soup.text)

    for i in range(len(site_json)):
    pubdebt = site_json[i]['pubdebt']
    publease = site_json[i]['publeaseroject']
    highmetalprice = site_json[i]['highmetalprice']


    try:
        Job.objects.create(
            pubdebt=pubdebt,
            publeaseroject=publeaseroject,
            highmetalprice=highmetalprice,

In the code below, I'm tryin to get previous low value to use. Each lines reference to next day like 1-1-2020, 1-02-2020 - 01-03-2020 - 01-04-2020 and my Data is like

"pubdebt":CA,"publeaseroject":145,"highmetalprice":110200,
"pubdebt":CA,"publeaseroject":145,"highmetalprice":130200,
"pubdebt":CA,"publeaseroject":145,"highmetalprice":150200,
"pubdebt":CA,"publeaseroject":145,"highmetalprice":180200,
"pubdebt":CA,"publeaseroject":145,"highmetalprice":120200,

My Expect is to get in every loop:

Pubdebt: Ca  (sticky value)
Publeaseproject:145 (sticky value)
highmetalprice:  110200  (first highmetalopenprice value)
HIGHMETALCLOSE: .... (it may blank because we have just one price for beggining day)

Second loop would be like :

Pubdebt: Ca  (sticky value)
Publeaseproject:145 (sticky value)
Highmetalopen:  110200  ( previous highmetalprice value)
HIGHMETALCLOSE: 130200 (current highmetalprice value)

Third loop :

Pubdebt: Ca  (current value)
Publeaseproject:145 (sticky value)
Highmetalopen:  130200  ( previous highmetalprice value)
HIGHMETALCLOSE: 150200 (current highmetalprice value)

and fourth like:

Pubdebt: Ca  (sticky value) 
Publeaseproject:145 (sticky value)
Highmetalopen:  150200  ( previous highmetalopenprice value )
HIGHMETALCLOSE: 180200 ( current highmetalopenprice value )

I tried something with find_previous but didnt figure out. How do I loop through all the previous\next price?

Thank you.


Solution

  • You should keep previous price in variable outside loop and use it in loop with current price. And at the end of loop you should assing current price as previous price to use it in next loop.

    Something similar to this

    previous_price = ''
    
    for item in site_json:
        pubdebt = item['pubdebt']
        publease = item['publeaseroject']
        highmetalprice = item['highmetalprice']
        
        print('price current:', highmetalprice)
        print('price previous:', previous_price)
        
        # ... work with both prices ...
        
        # assign current to previous for next loop
        previous_price = highmetalprice