I want to get " 24.8 " data from code at belove.
<div class="anlik-sicaklik">
<div class="anlik-sicaklik-deger ng-binding" ng-bind="sondurum[0].sicaklik | comma">
24,8
::after
</div>
<div class="anlik-sicaklik-havadurumu">
<div class="anlik-sicaklik-havadurumu-ikonu">
My codes are ;
import requests
from bs4 import BeautifulSoup
r = requests.get("https://mgm.gov.tr/tahmin/il-ve-ilceler.aspx?il=ANTALYA&ilce=KUMLUCA")
soup = BeautifulSoup(r.content, "lxml")
sicaklik = soup.find('div', {'class':'anlik-sicaklik-deger'})
print(sicaklik)
after running value is ;
<div class="anlik-sicaklik-deger" ng-bind="sondurum[0].sicaklik | comma">
</div>
could you please help me to get 24,8 value.
CodePudding user response:
Your question concern more about parsing string than web-page. So it is better, once found the tag with bs4, to parse the string with some regex.
The matching condition ([0-9] ,[0-9])
is one or more number separated by a ,
and then a number again.
Notice the the final result, nr
, is a string, to make it a number you should use float(nr.replace(',', '.'))
.
from bs4 import BeautifulSoup
import re
html = """
<div class="anlik-sicaklik-deger ng-binding" ng-bind="sondurum[0].sicaklik | comma">
24,8
::after
</div>
"""
soup = BeautifulSoup(html, 'lxml')
div = soup.find('div', class_='anlik-sicaklik-deger', string=True)
# get text
text = str(div.string).strip()
# regex
nr = re.search(r'([0-9] ,[0-9])', text).group(0)
print(nr)
Output
24,8
CodePudding user response:
Actually, the value is dynamic.That's why to grab the value, I have to use selenium with bs4.
Code:
import requests
from bs4 import BeautifulSoup
import time
from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
r = "https://mgm.gov.tr/tahmin/il-ve-ilceler.aspx?il=ANTALYA&ilce=KUMLUCA"
driver.get(r)
time.sleep(8)
soup = BeautifulSoup(driver.page_source, 'lxml')
sicaklik = soup.select_one('div.anlik-sicaklik-deger')
print(sicaklik.text)
Output: According to my local
27,8