0

I have the code below, the code below will take the value from the pluang.com site, but I want to make 2 colors so that I don't get confused and it's easy to understand.

I want if the scraped value is below (-) 860,000 (mis. 850.000) then the print must be red, if it is above (+) 860,000 (mis. 870.000) then the print must be green.

Can anyone help? Thank you.

from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
from datetime import datetime
import time
import os

class bcolors:
    SELL = '\033[92m'
    BUY  = '\033[31;1m'
    
os.system("clear")

while True:
  url = "https://pluang.com/produk/pluang-emas"
  UserAgent = Request(url, headers={'User-Agent':'Mozilla/5.0'})
  html = urlopen(UserAgent)
  data = BeautifulSoup(html, "html.parser")

  items = data.findAll("h1", {"id":"gold-price"})
  for item in items:
     print(bcolors.BUY + " SELL GOLD:",item.get_text().strip(),"-",datetime.now())
     time.sleep(59)
  • Does this help? https://stackoverflow.com/a/287944/2681662 – MSH Nov 05 '21 at 18:22
  • Your code looks fundamentally correct. What happened when you tried this? Note that console color support varies by operating system. Which are you using? – Tim Roberts Nov 05 '21 at 18:23
  • @MSH 6 Sorry sir, but that's different from what I described. – Kabar Kabari Nov 05 '21 at 18:24
  • @Tim Roberts So the above code is correct, there are just a few that I need and can you add them? I need a green print and a red print, if the data I scrapped from pluang.com is worth 860,000 and above it will be green when printed, but if it is below 860,000 it will be red when printed, how do I do that? – Kabar Kabari Nov 05 '21 at 18:26
  • 1
    So, you're not asking "how do I print colors", you're asking the much simpler question "how can I tell whether the price is above or below 860,000"? – Tim Roberts Nov 05 '21 at 18:41

2 Answers2

0

You already have the class to color you terminal. You will need to somehow get the value (which you are displaying it) check if it's bigger or smaller then your condition then print accordingly.

In your code item.get_text() printing something like: Rp860.530/g. I believe here Rp is currency and g would be gram. Let's get rid of them:

import re
numbers = rr = list(map(float, re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", item.get_text())))

see: https://stackoverflow.com/a/4289348/2681662

This will return all numbers in your text as a list.

Now we can check the value if it's smaller or bigger then our threshold.

from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
from datetime import datetime
import time
import os
import re


class bcolors:
    BUY = '\033[92m'
    SELL = '\033[31;1m'


os.system("clear")

while True:
    url = "https://pluang.com/produk/pluang-emas"
    UserAgent = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    html = urlopen(UserAgent)
    data = BeautifulSoup(html, "html.parser")

    items = data.findAll("h1", {"id": "gold-price"})
    for item in items:
        rr = list(map(float, re.findall("[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?", item.get_text())))
        if rr[0] < 860:
            print(bcolors.SELL + " SELL GOLD:", item.get_text().strip(), "-", datetime.now())
        else:
            print(bcolors.BUY + " BUY GOLD:", item.get_text().strip(), "-", datetime.now())

        time.sleep(59)

I'm not sure if the red and green part is correctly written. You can change it anyways.

MSH
  • 1,743
  • 2
  • 14
  • 22
0

I think this is what you're after. Your question definitely implied that you were asking how to print colors. You would have had more focused answers if you had just said you needed to know how to extract the price.

There's only one price returned here, so the inner loop seems silly.

from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
from datetime import datetime
import time
import os

class bcolors:
    SELL = '\033[92m'
    BUY  = '\033[31;1m'
    WHITE = '\033[37m'
    
url = "https://pluang.com/produk/pluang-emas"
while True:
    UserAgent = Request(url, headers={'User-Agent':'Mozilla/5.0'})
    html = urlopen(UserAgent)
    data = BeautifulSoup(html, "html.parser")

    item = data.findAll("h1", {"id":"gold-price"})[0].get_text()
    price = int(item.strip('Rp/g').replace('.',''))
    if price > 860000:
        print(bcolors.SELL + " SELL GOLD:",item,"-",datetime.now())
    else:
        print(bcolors.BUY + "  BUY GOLD:",item,"-",datetime.now())
    print(bcolors.WHITE,end='')
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30