Skip to content

Instantly share code, notes, and snippets.

@onurmatik
Created August 5, 2019 10:39
Show Gist options
  • Save onurmatik/80905285e5ba2b3a89679de32fb3a177 to your computer and use it in GitHub Desktop.
Save onurmatik/80905285e5ba2b3a89679de32fb3a177 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dernek bilgilerinin cekilmesi\n",
"\n",
"Turkiye'de kayitli derneklerin bilgilerini cekmek icin bir Python 3 scriptini adim adim anlatiyorum. Veriler icin kaynagimiz Derbis (https://derbis.dernekler.gov.tr/) sistemi."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python'un kendi kutuphanelerinin yanisira, HTTP request'lerini kolay yapabilmek icin `requests` kutuphanesini kullanacagiz. Eger ortaminizda kurulu degil ise kuralim."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install requests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kullanacagimiz kutuphaneleri import edelim. "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import csv\n",
"import requests"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dernek bilgilerini il bazinda cekip, bir `.csv` dosyasina yazacagiz. Asagidaki fonksiyon, plaka numarasi verilen bir ildeki dernek bilgilerini cekip verdigimiz dosyaya ekleyecek. "
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def get_il(il, csv_writer):\n",
" data = json.dumps({\n",
" 'secilenTeskilatPk': il,\n",
" 'secilenIlcePk': '999999999',\n",
" 'neviler': '0',\n",
" 'altneviler': '0',\n",
" })\n",
" p = 1\n",
" while True:\n",
" response = requests.post(\n",
" 'https://derbis.dernekler.gov.tr/IstatistikDerneklerWeb/GetIlFaaliyetDernek',\n",
" data={\n",
" 'page': p,\n",
" 'pageSize': 1000,\n",
" 'serializedData': data,\n",
" },\n",
" verify=False,\n",
" )\n",
" if reponse.json()['Errors']:\n",
" # hata olmus; yazalim ve donguden cikalim\n",
" print(reponse.json()['Errors'])\n",
" break\n",
" for dernek in response.json()['Data']:\n",
" row = [\n",
" dernek['Ad'],\n",
" dernek['Sayi'],\n",
" dernek['Oran'],\n",
" dernek['Plaka'],\n",
" dernek['AnaNevisi'],\n",
" dernek['telNo'],\n",
" dernek['Mail'],\n",
" dernek['WebSitesi'],\n",
" dernek['FkSisKodPkNevi'],\n",
" dernek['FKSisTeskilatPk'],\n",
" dernek['SisKod'],\n",
" dernek['KurumAd'],\n",
" dernek['KurumAdres'],\n",
" dernek['Sira'],\n",
" dernek['BaskanAdSoyad'],\n",
" dernek['FkSisCityInfoPk'],\n",
" dernek['AltNevisString'],\n",
" dernek['IsActive'],\n",
" dernek['DtOlusturma'],\n",
" dernek['DtDegistirme'],\n",
" dernek['Olusturan'],\n",
" dernek['Degistiren'],\n",
" ]\n",
" csv_writer.writerow(row)\n",
" if p * 1000 >= response.json()['Total']:\n",
" # verilen il icin tum sayfalar cekildi\n",
" break\n",
" p += 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Verileri kaydedecegimiz dosyayi olusturup kolon basliklarini yazdiralim. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"with open('dernekler.csv', 'w') as f:\n",
" w = csv.writer(f)\n",
" w.writerow([\n",
" 'Ad',\n",
" 'Sayi',\n",
" 'Oran',\n",
" 'Plaka',\n",
" 'AnaNevisi',\n",
" 'telNo',\n",
" 'Mail',\n",
" 'WebSitesi',\n",
" 'FkSisKodPkNevi',\n",
" 'FKSisTeskilatPk',\n",
" 'SisKod',\n",
" 'KurumAd',\n",
" 'KurumAdres',\n",
" 'Sira',\n",
" 'BaskanAdSoyad',\n",
" 'FkSisCityInfoPk',\n",
" 'AltNevisString',\n",
" 'IsActive',\n",
" 'DtOlusturma',\n",
" 'DtDegistirme',\n",
" 'Olusturan',\n",
" 'Degistiren',\n",
" ])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Butun illerdeki derneklerin bilgilerini cekmek icin asagidaki donguyu kullanabilirsiniz."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open('dernekler.csv', 'a') as f:\n",
" writer = csv.writer(f)\n",
" for il in range(1, 82):\n",
" get_il(il, writer)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Butun illere ihtiyaciniz yoksa veya cok vakit aldigi icin bolmeyi tercih ediyorsaniz, dongu kullanmadan illeri tek tek cekmeyi tercih edebilirsiniz. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Burada anlatilan islemler, kaynaktaki degisikliklere gore guncelligini kaybedebilir. Bir hata fark ederseniz lutfen bana (onur [at] genelizleyici.com) bildirmekten cekinmeyin. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment