I try to make a python script that gets the dam occupancy rates from a website. Here is the code:
JavaScript
x
12
12
1
baraj_link = "https://www.turkiye.gov.tr/istanbul-su-ve-kanalizasyon-idaresi-baraj-doluluk-oranlari"
2
response = requests.get(baraj_link)
3
soup = BeautifulSoup(response.text, "lxml")
4
5
values_list = []
6
values = soup.find_all('dl',{re.compile('compact')})
7
for val in values:
8
text = val.find_next('dt').text
9
value = val.text
10
values_list.append((text,value))
11
baraj = values_list[0][1]
12
The output is like this:
JavaScript
1
5
1
Tarih
2
18/01/2021
3
Genel Doluluk Oranı (%)
4
29,48
5
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
.
JavaScript
1
2
1
value = val.text.split()[-1]
2