import requests
import re
def get_coordinates_from_url(url):
response = requests.get(url)
regex = r"\[\[-?\d+\.\d+,-?\d+\.\d+\](?:,\[-?\d+\.\d+,-?\d+\.\d+\])+\]"
matche = re.search(regex, response.text)
if not matche:
return None
coordinates = eval(matche.group(0))
return coordinates
response = requests.get("https://reserves-naturelles.org/reserves-naturelles/")
regex = r"
"
matches = re.findall(regex, response.text)
kml_file = open("reserves_naturelles.kml", "w", encoding="utf-8")
kml_file.write("""
RN
""")
for i, match in enumerate(matches):
regex = r"title=\"([^\"]+)\""
full_name = re.search(regex, match)
if not full_name:
print(f"Full name not found in the match: {match}")
continue
full_name = full_name.group(1)
regex = r"class=\"rsv_nom\">([^<]+)<"
name = re.search(regex, match)
if not name:
print(f"Name not found in the match: {match}")
continue
name = name.group(1)
regex = r"href=\"([^\"]+)\""
url = re.search(regex, match)
if not url:
print(f"URL not found in the match: {match}")
continue
url = url.group(1)
coordinates = get_coordinates_from_url(url)
if not coordinates:
print(f"Coordinates not found for URL: {url}")
continue
kml_file.write(f"""
{name}
{full_name}
{url}
""")
for coordinate in coordinates:
kml_file.write(f" {coordinate[0]},{coordinate[1]},0\n")
kml_file.write("""
""")
print(f"Processed: {name} ({i+1: 3d}/{len(matches)})")
kml_file.write("""
""")