-
-
Save eoner/b5e9cc574c527b7864a1 to your computer and use it in GitHub Desktop.
milliyetemlak ve ilceharitası sitelerinden mahalle sınır verilerini indirmek için...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Newtonsoft.Json; | |
namespace dotMapCS | |
{ | |
public class Scraper | |
{ | |
public static void ScrapeMilliyetEmlak() | |
{ | |
string baseDir = "milliyetemlak.com"; | |
Directory.CreateDirectory(baseDir); | |
using (WebClient wc = new WebClient() { Encoding = Encoding.UTF8 }) | |
{ | |
string jsonString = string.Empty; | |
for (int cityID = 1; cityID < 86; cityID++) | |
{ | |
try | |
{ | |
jsonString = wc.DownloadString(string.Format("http://www.milliyetemlak.com/ashx/mapdatav5/townsv3-{0}.txt", cityID)); | |
dynamic ilceler = JsonConvert.DeserializeObject(jsonString); //ilceler is an array | |
Console.WriteLine("Downloaded: {0}", ilceler[0].CityName.Value); | |
File.WriteAllText(Path.Combine(baseDir, string.Format("{0:00}_ilceler.json", cityID, ilceler[0].CityName.Value)), jsonString); | |
foreach (var ilce in ilceler) | |
{ | |
Console.WriteLine("---Downloading: {0}", ilce.TownName.Value); | |
jsonString = wc.DownloadString(string.Format("http://www.milliyetemlak.com/ashx/mapdatav5/dists/distsv3-{0}.txt", ilce.TownID.Value)); | |
File.WriteAllText(Path.Combine(baseDir, string.Format("{0:00}_{1}.json", cityID, ilce.TownID.Value)), jsonString); | |
} | |
} | |
catch | |
{ | |
Console.WriteLine("Skipped cityID:{0}", cityID); | |
} | |
} | |
} | |
} | |
public static void ScrapeIlceHaritasi() | |
{ | |
string baseDir = "ilceharitasi.com"; | |
Directory.CreateDirectory(baseDir); | |
string citiesFile = @"..\..\data\iller.txt"; // her satırda ilin plaka kodu <boşluk> İL_ADI (türkçe büyük harf) | |
string[] lines = File.ReadAllLines(citiesFile); | |
bool skip = false; string skipStop = "1"; // to resume, set skip=true, skipStop="plaka kodu" | |
foreach (string line in lines) | |
{ | |
if (string.IsNullOrEmpty(line)) continue; | |
var tokens = line.Split(' '); | |
if (tokens.Length == 0) continue; | |
if (skip && tokens[0] != skipStop) continue; // check if resume needed | |
else skip = false; | |
using (WebClient wc = new WebClient() { Encoding = Encoding.UTF8 }) | |
{ | |
string cityID = tokens[0]; | |
string jsonString = string.Empty; | |
try | |
{ | |
jsonString = wc.DownloadString(string.Format("http://ilceharitasi.com/home/getIlcelerByIl?ilId={0}", cityID)); | |
File.WriteAllText(Path.Combine(baseDir, string.Format("{0}_ilceler.json", cityID)), jsonString); | |
} | |
catch | |
{ | |
Console.WriteLine("skipping: " + tokens[1]); | |
continue; | |
} | |
dynamic ilceler = JsonConvert.DeserializeObject(jsonString); | |
foreach (var ilce in ilceler) | |
{ | |
try | |
{ | |
Console.WriteLine("il:{0} ilce:{1}", tokens[1], ilce.ad); | |
wc.DownloadFile(string.Format("http://ilceharitasi.com/home/getSecimSonuc?ilceId={0}&ilce_adi={1}", ilce.id, ilce.ad), | |
Path.Combine(baseDir, string.Format("{0}.json", ilce.id))); | |
Thread.Sleep(500); // wait for the server | |
} | |
catch | |
{ | |
Console.WriteLine("error in: " + ilce.ad); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment