Skip to content
Advertisement

BeautifulSoup choosing a line from output

I try to make a python script that gets the dam occupancy rates from a website. Here is the code:

baraj_link = "https://www.turkiye.gov.tr/istanbul-su-ve-kanalizasyon-idaresi-baraj-doluluk-oranlari"
response = requests.get(baraj_link)
soup = BeautifulSoup(response.text, "lxml")

values_list = []
values = soup.find_all('dl',{re.compile('compact')})
for val in values:
    text = val.find_next('dt').text
    value = val.text
    values_list.append((text,value))
baraj = values_list[0][1]

The output is like this:

Tarih
18/01/2021
Genel Doluluk Oranı (%)
29,48

Genel Doluluk Oranı means occupancy rate. I need the value of occupancy rate which writes in next line like 29,48. How can I get this value from output?

Advertisement

Answer

Cause question and expected result is not that clearly as mentioned I just point to the output of occupancy rate.

Get the value by split() the string

Prerequisite is that the value is always the last substring in string.

value = val.text.split()[-1]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement