2
import requests
from bs4 import BeautifulSoup

html = requests.get("https://www.haremaltin.com/canli-piyasalar/")

soup = BeautifulSoup(html.content)

atalira = soup.findall(?????)
for gold in atalira:
  price = gold.text
  print(price)

Hello everyone, if you go to page https://www.haremaltin.com/canli-piyasalar/ In "Altın Fiyatları" you will see "Eski Ata". I want to insert one of those values into ?????? part of my python code and it is a little bit challenging for me. Thank you for your time in advance. Below you can see that html codes and value that I want to insert

<span class="item end price"><span class="arrowWrapper"><!----> <!----></span>
                                    3.327
                                </span>

Edit: I have found a way

from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time 

# pip install selenium
# apt-get update # to update ubuntu to correctly run apt install
# apt install chromium-chromedriver
# cp /usr/lib/chromium-browser/chromedriver /usr/bin
# use command above if you code on google colab

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')

site = 'https://www.haremaltin.com/altin-fiyatlari'

wd = webdriver.Chrome('chromedriver', options=options)

wd.get(site)

time.sleep(5) # give chrome 5 seconds to load the page 

html = wd.page_source

df = pd.read_html(html)

gold = df[1][2][15] # table 1, column 2, row 15, the value I want
gold = int((float(gold))*1000)
# it was a float and even more float value that I got, something like 
# 3.252,000, so I tried to convert it into int so code above the 
# solution that I found
eyup.tatar
  • 75
  • 8

1 Answers1

0

I'm not sure you can do it this way. The best way is to get the API for this site you want and go from there. If you can't get it, find a different site. Here is a sample code I made a while back ago.

import re
import http.client

def gold_price():

    conn = http.client.HTTPSConnection("www.goldapi.io")
    payload = ''
    headers = {
    'x-access-token': 'goldapi-aq2kfluknfhfjz4-io',
    'Content-Type': 'application/json'
    }
    conn.request("GET", "/api/XAU/USD", payload, headers)
    res = conn.getresponse()
    data = res.read()
    data.decode("utf-8")
    txt = data.decode("utf-8")
    pattern = re.search(r'"price":\d\d\d\d',txt)

    # pattern = re.findall(r'\d\d\d\d',txt)
    print(pattern)
gold_price()
Robin Sage
  • 969
  • 1
  • 8
  • 24
  • If you find the answer useful, please vote up and mark it as checked. Thanks. – Robin Sage Jul 24 '21 at 19:02
  • I still wonder how I can insert that value. There must be a way – eyup.tatar Jul 25 '21 at 09:22
  • If you look at the page source, you can't find the price. You'll see containers to the dashboards, which are then connected to the API. As far as I know, the only way is to do through the API. – Robin Sage Jul 25 '21 at 18:08