-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
260 lines (225 loc) · 10.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System.Collections.Generic;
using System;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using HtmlAgilityPack;
using System.Net.Http;
using System.Reflection.Metadata;
namespace UrlHouses
{
internal class Program
{
static void Main(string[] args)
{
// List<String> urls = getAllURL();
CrawlDataUsingAgilityPack();
Console.ReadLine();
}
static int countHouse = 0;
//private static List<String> getAllURL()
//{
// List<string> listUrl = new List<string>();
// IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(Environment.CurrentDirectory);
// driver.Manage().Window.Minimize();
// driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(20);
// int countThread = 0;
// int countHouse = 0;
// // get houses URLs
// try
// {
// driver.Url = "https://nha.chotot.com/mua-ban-bat-dong-san";
// int totalOfPages = 1;
// while (true)
// {
// driver.Url = String.Format("https://nha.chotot.com/mua-ban-bat-dong-san?page={0}", totalOfPages);
// bool existPage = driver.PageSource.Contains("NotFound_notFoundWrapper__1Erzd");
// if (!existPage)
// {
// new WebDriverWait(driver, new TimeSpan(0, 0, 1500)).Until(c => c.FindElement(By.ClassName("list-view")));
// IList<IWebElement> aTags = driver.FindElements(By.XPath("//div[@class='list-view']/div/div/ul/div/li/a"));
// int ccountHouse = 0;
// foreach (IWebElement element in aTags)
// {
// string urlHouse = element.GetAttribute("href");
// if (urlHouse != null)
// {
// listUrl.Add(urlHouse);
// }
// ccountHouse++;
// if(countHouse > 50)
// {
// Thread thread = new Thread(()=>CrawlData(listUrl, countThread));
// thread.Start();
// countThread++;
// }
// }
// totalOfPages++;
// if (totalOfPages > 2)
// {
// break;
// }
// }
// }
// driver.Close();
// driver = new ChromeDriver();
// driver.Dispose();
// }
// catch (Exception e)
// {
// Console.WriteLine(e.Message);
// }
// return listUrl;
//}
//Crawl all the houses
private static async Task CrawlAllData(HtmlNodeCollection houses, int count)
{
Console.WriteLine("-------------------------------------");
Console.WriteLine("Start thread: " + count);
int dem = 0;
List<Task<String>> awaitingTasks = new List<Task<string>>();
foreach (var house in houses)
{
dem++;
var node = house.SelectSingleNode(".//a");
var task = CrawlAHouse(node, count);
awaitingTasks.Add(task);
await Task.WhenAny(awaitingTasks);
}
await Task.WhenAll(awaitingTasks);
Console.WriteLine("-------------------------------------");
Console.WriteLine("End thread: " + count);
}
//Crawl each house
private static async Task<string> CrawlAHouse(HtmlNode node, int thread)
{
return await Task.Run(() =>
{
// GetInforUsingSelenium(url, dem);
GetInforEachHouseUsingAgilityPack(node, thread);
return "Done";
});
}
//Get information each house
private static void CrawlDataUsingAgilityPack()
{
try
{
var htmlWeb = new HtmlWeb();
string url = "https://nha.chotot.com/mua-ban-bat-dong-san-phuong-chanh-nghia-thanh-pho-thu-dau-mot-binh-duong";
HtmlDocument document = htmlWeb.Load(url);
int totalOfPages = 1;
int totalOfHouses = 0;
int countThread = 0;
while (true)
{
url = String.Format("https://nha.chotot.com/mua-ban-bat-dong-san?page={0}", totalOfPages);
document = htmlWeb.Load(url);
bool isFinalPage = document.ToString().Contains("NotFound_notFoundWrapper__1Erzd");
if (!isFinalPage)
{
var houses = document.DocumentNode.SelectNodes("//div[@class='list-view']/div/div/ul/div/li");
countThread++;
Thread thread = new Thread(() => CrawlAllData(houses, countThread));
thread.Start();
totalOfPages++;
}
else
{
break;
}
if (totalOfPages > 100)
{
break;
}
}
}
catch (HttpRequestException e)
{
Console.WriteLine(e.Message);
}
}
private static void GetInforEachHouseUsingAgilityPack(HtmlNode node, int thread)
{
countHouse++;
if (node != null)
{
string uri = "https://nha.chotot.com" + node.Attributes["href"].Value;
var htmlWeb = new HtmlWeb();
HtmlDocument document = htmlWeb.Load(uri);
bool isExisted = document.ToString().Contains("NotFound_notFoundWrapper__1Erzd");
if (!isExisted)
{
//html of a house link
HttpClient httpHouse = new HttpClient();
httpHouse.BaseAddress = new Uri(uri);
var res = httpHouse.GetStringAsync("").Result;
//get information of house
//.Replace("<h1 class=\"AdDecription_adTitle__2I0VE\" itemProp=\"name\"> <!-- -->", "");
var title = Regex.Match(res, @"(?<=<h1 class=\""AdDecription_adTitle__2I0VE\"" [\b\D\W]*\-\->)(.*)(?=<\/h1>)", RegexOptions.Singleline).Value;
//var address = Regex.Match(res, @"");
var acreage = Regex.Match(res, @"(?<=\""AdParam_adParamValue__1ayWO\""\>)(.*?)(?=<\/span>)", RegexOptions.Singleline).Value;
var price = Regex.Match(res, @"(?<=<span class=\""AdDecription_price__O6z15\""><span itemProp=\""price\"">)[\w\D]*(?=<span class=\""AdDecription_squareMetre__2KYh8\"">)", RegexOptions.Singleline).Value;
var description = Regex.Match(res, @"(?<=<p class=\""AdDecription_adBody__1c8SG\"" [\b\D\W]*tion\"">)(.*)(?=<\/p>)", RegexOptions.Singleline).Value;
Console.WriteLine("In thread: " + thread + ", house " + countHouse + " :" + title + "\n" + acreage + "\n" + price + "\n");
}
}
}
private static void GetInforUsingSelenium(string urlHouse, int dem)
{
try
{
IWebDriver childDriver = new OpenQA.Selenium.Chrome.ChromeDriver(Environment.CurrentDirectory);
childDriver.Manage().Window.Minimize();
childDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(1000);
try
{
childDriver.Url = urlHouse;
dem++;
//get house's information
var title = childDriver.FindElement(By.CssSelector(".AdDecription_adDecriptionWrapper__36qgN>.AdDecription_adTitle__2I0VE")).Text;
var area = childDriver.FindElement(By.CssSelector(".ct-detail .AdDecription_adDecriptionWrapper__36qgN .AdDecription_squareMetre__2KYh8")).Text;
var address = childDriver.FindElement(By.CssSelector(".ct-detail .AdParam_address__1pmCR>span")).Text;
address = Regex.Match(address, @"[\w\D]*(?=.{10})", RegexOptions.Singleline).Value;
var price = childDriver.FindElement(By.CssSelector(".ct-detail .AdDecription_adDecriptionWrapper__36qgN .AdDecription_price__O6z15>span")).Text;
price = Regex.Match(price, @"[0-9]*\s.*(?=\-)", RegexOptions.Singleline).Value;
var telephone = childDriver.FindElement(By.CssSelector("div.LeadButtonSlide_wrapperLeadButton__2Ytpy")).GetAttribute("innerHTML");
telephone = Regex.Match(telephone.ToString(), @"(?<=\""tel\:)[\d]*(?=\"">)", RegexOptions.Singleline).Value;
var description = childDriver.FindElement(By.CssSelector(".ct-detail .AdDecription_adDecriptionWrapper__36qgN .AdDecription_adBody__1c8SG")).Text;
//get house's image
var btnImg = childDriver.FindElements(By.CssSelector(".AdImage_adImageWrapper__15vl2 .slick-dots>li")).Count;
List<string> imgs = new List<string>();
while (btnImg > 0)
{
IWebElement btn = childDriver.FindElement(By.CssSelector(".AdImage_adImageWrapper__15vl2 .slick-slider .AdImage_Next__26U9L"));
btn.Click();
btnImg--;
var imgTags = childDriver.FindElement(By.CssSelector(".ct-detail .AdImage_sliderImage__3REqQ .AdImage_imageWrapper__1yIxE")).GetAttribute("innerHTML");
imgTags = Regex.Match(imgTags, @"(?<=src=\"")https:\/\/cdn\.chotot\.com(.*)\.jpg(?=\"")", RegexOptions.Singleline).Value;
imgs.Add(imgTags);
}
Console.WriteLine("\n\n" + title + "\n" + address + "\n" + area + "\n" + price + "\n" + telephone + "\n" + description + "\n\n");
childDriver.Close();
Thread.Sleep(2000);
childDriver.Dispose();
Console.WriteLine(dem);
}
catch (Exception ec)
{
Console.WriteLine("Child driver " + ec.Message);
}
}
catch (Exception e)
{
}
}
}
}