static void AllSheng() { JumonyParser parser = new JumonyParser(); IEnumerable <IHtmlElement> list = load(prefix + "index.html").Find("tr.provincetr td a"); foreach (var ele in list) { //遍历所有的省 Sheng sheng = new Sheng(); sheng.name = ele.InnerText().Trim(); sheng.url = ele.Attribute("href").Value().Trim(); sheng.code = sheng.url.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0]; string path = tmp + "allprovince.txt"; System.IO.File.AppendAllLines(path, new string[] { sheng.name + "#" + sheng.code + "#" + sheng.url }); } }
static void Sheng(string code) { String[] lines = System.IO.File.ReadAllLines(tmp + "\\allprovince.txt"); List <Sheng> shengs = new List <Sheng>(); foreach (var line in lines) { Sheng t = new Sheng(); t.code = line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)[1]; t.name = line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)[0]; t.url = line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)[2]; shengs.Add(t); } Sheng sheng = null; foreach (var i in shengs) { if (i.code == code) { sheng = i; break; } } if (sheng == null) { return; } IEnumerable <IHtmlElement> citys = parser.LoadDocument(prefix + sheng.url).Find("tr.citytr"); filepath = tmp + sheng.name + ".txt"; Console.WriteLine(sheng.name + "---" + sheng.code.PadRight(12, '0') + "---" + sheng.url); System.IO.File.AppendAllLines(filepath, new string[] { sheng.name + "#" + sheng.code.PadRight(12, '0') + "#" + sheng.url }); foreach (var city in citys) { //遍历所有的市 IEnumerable <IHtmlElement> a = city.Find("td a"); Shi shi = new Shi(); shi.url = a.ToList <IHtmlElement>()[1].Attribute("href").Value().Trim(); shi.code = shi.url.Replace(sheng.code + "/", "").Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0].PadRight(12, '0'); shi.name = a.ToList <IHtmlElement>()[1].InnerText().Trim(); sheng.shis.Add(shi); Console.WriteLine(shi.name + "---" + shi.code + "---" + shi.url); System.IO.File.AppendAllLines(filepath, new string[] { "\t" + shi.name + "#" + shi.code + "#" + shi.url }); IEnumerable <IHtmlElement> li = load(prefix + shi.url).Find("tr.countytr"); foreach (var tr in li) { //遍历所有的县 IEnumerable <IHtmlElement> td = tr.Find("td"); Xian xian = new Xian(); if (td.Find("a").Count() > 0) { xian.url = td.ToList <IHtmlElement>()[0].Find("a").First <IHtmlElement>().Attribute("href").Value().Trim(); xian.code = td.ToList <IHtmlElement>()[0].Find("a").First <IHtmlElement>().InnerText().Trim(); xian.name = td.ToList <IHtmlElement>()[1].Find("a").First <IHtmlElement>().InnerText().Trim(); shi.xians.Add(xian); Console.WriteLine("\t" + xian.name + "---" + xian.code + "---" + xian.url); System.IO.File.AppendAllLines(filepath, new string[] { "\t\t" + xian.name + "#" + xian.code + "#" + xian.url }); IEnumerable <IHtmlElement> xiangs = load(prefix + sheng.code + "/" + xian.url).Find("tr.towntr"); foreach (var j in xiangs) { //遍历所有的乡 Xiang xiang = new Xiang(); xiang.code = j.Find("a").ToList <IHtmlElement>()[0].InnerText().Trim(); xiang.name = j.Find("a").ToList <IHtmlElement>()[1].InnerText().Trim(); xiang.url = j.Find("a").ToList <IHtmlElement>()[0].Attribute("href").Value().Trim(); xian.xiangs.Add(xiang); Console.WriteLine("\t\t" + xiang.name + "---" + xiang.code + "---" + xiang.url); System.IO.File.AppendAllLines(filepath, new string[] { "\t\t\t" + xiang.name + "#" + xiang.code + "#" + xiang.url }); string url = prefix + sheng.code + "/" + xian.url; url = url.Substring(0, url.LastIndexOf("/")) + "/" + xiang.url; IEnumerable <IHtmlElement> cuns = null; cuns = load(url).Find("tr.villagetr"); foreach (var ko in cuns) { //遍历所有的村 Cun cun = new Cun(); cun.code = ko.Find("td").ToList <IHtmlElement>()[0].InnerText().Trim(); cun.name = ko.Find("td").ToList <IHtmlElement>()[2].InnerText().Trim(); xiang.cuns.Add(cun); Console.WriteLine("\t\t\t\t" + cun.name + "---" + cun.code); System.IO.File.AppendAllLines(filepath, new string[] { "\t\t\t\t" + cun.name + "#" + cun.code }); } System.Threading.Thread.Sleep(1000); } } else { xian.code = td.ToList <IHtmlElement>()[0].InnerText().Trim(); xian.name = td.ToList <IHtmlElement>()[1].InnerText().Trim(); shi.xians.Add(xian); Console.WriteLine("\t" + xian.name + "---" + xian.code + "---" + xian.url); System.IO.File.AppendAllLines(filepath, new string[] { "\t\t" + xian.name + "#" + xian.code + "#" + xian.url }); } } System.Threading.Thread.Sleep(3000); } }