-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
275 lines (186 loc) · 9.26 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
using HtmlAgilityPack;
using System.Net;
using OfficeOpenXml;
// sosdfsfds
namespace Program
{
public class Program {
public async static Task Main(){
var (productName, Category, productsNbr) = GetInfoFromUser();
List<ProductInfo> result = await LoadProductsInfo(productName, Category, productsNbr);
// foreach(ProductInfo productInfo in result){
// System.Console.WriteLine(productInfo);
// }
LoadDataOnExcelFile(result);
}
public static string ConstructUrl(string productName, string Category, int pageNbr = 1){
string url = $"https://www.amazon.com/s?k={productName}&i={Category}-intl-ship&page={pageNbr}&ref=sr_pg_2";
return url;
}
public async static Task<List<ProductInfo>> LoadProductsInfo(string productName, string Category, int ProductsNbr = 16){
int page = 1;
List<ProductInfo> productInfos = new();
do{
string url = ConstructUrl(productName, Category, page);
string result = await Program.LoadHtmlPage(url);
if(result.Length == 0)
throw new Exception("problem with loading page");
HtmlDocument htmlDoc = new();
htmlDoc.LoadHtml(result);
// Searched Result container
HtmlNode SearchedResultContainer = htmlDoc.DocumentNode.SelectSingleNode("//*[@id=\"search\"]/div[1]/div[1]/div/span[1]/div[1]");
// System.Console.WriteLine(SearchedResultContainer.Name);
// list of target product
IEnumerable<HtmlNode> SearchedResult = SearchedResultContainer.ChildNodes.Where(c => c.Name == "div" && c.Attributes.Contains("data-component-type") );
// foreach(var h in SearchedResult)
// System.Console.WriteLine(h.Attributes["data-asin"].Value);
// product Infos list
foreach(HtmlNode productNode in SearchedResult){
// LoadProductInfo(productNode);
productInfos.Add(LoadProductInfo(productNode));
// System.Console.WriteLine(productNode.Attributes["data-asin"].Value);
}
page++;
}while(productInfos.Count() < ProductsNbr);
return productInfos.Take(ProductsNbr).ToList();
}
public static ProductInfo LoadProductInfo(HtmlNode productNode){
string? Title = productNode.SelectSingleNode($".//div/div/span/div/div/div/div[2]/div/div/div[1]/h2/a/span")?.InnerText;
string? Price = productNode.SelectSingleNode(".//div/div/span/div/div/div/div[2]/div/div/div[3]/div[1]/div/div[1]/div[2]/div[1]/a/span/span[1]")?.InnerHtml;
string? ImageUrl = productNode.SelectSingleNode(".//div/div/span/div/div/div/div[1]/div/div[2]/div/span/a/div/img")?.Attributes["src"]?.Value;
return new ProductInfo(Title, Price, ImageUrl);
}
public static async Task<String> LoadHtmlPage(string url){
string result = "";
HttpClientHandler handler = new HttpClientHandler();
// Set user agent header to mimic a real browser
handler.DefaultProxyCredentials = CredentialCache.DefaultCredentials;
handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
// handler.AutomaticDecompression = DecompressionMethods.GZip;
handler.AllowAutoRedirect = true;
handler.UseCookies = true;
handler.CookieContainer = new CookieContainer();
handler.UseDefaultCredentials = false;
using HttpClient httpClient = new(handler);
httpClient.DefaultRequestHeaders.Add(
"User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
);
try{
var response = await httpClient.GetAsync(url);
response.EnsureSuccessStatusCode();
// string result
result = response.Content.ReadAsStringAsync().Result;
}catch(HttpRequestException e){
System.Console.WriteLine($"Exception : {e.Message}");
}
return result;
}
public static void LoadDataOnExcelFile(List<ProductInfo> productInfos, string ProductName = "productX"){
try{
using (ExcelPackage excelPackage = new ExcelPackage())
{
// Add a worksheet to the package
ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet1");
// Populate the worksheet with data
worksheet.Cells["A1"].Value = "ProductTitle";
worksheet.Cells["B1"].Value = "ProductPrice";
worksheet.Cells["C1"].Value = "ImageUrl";
for(int i = 2; i < productInfos.Count() + 2; i++){
var (Title, Price, ImageUrl) = productInfos[i - 2];
worksheet.Cells[$"A{i}"].Value = Title;
worksheet.Cells[$"B{i}"].Value = Price;
worksheet.Cells[$"C{i}"].Value = ImageUrl;
}
string date = DateTime.UtcNow.ToLongDateString();
// Save the Excel package to a file
string filePath = $"example-{ProductName}-{date}.xlsx";
excelPackage.SaveAs(new System.IO.FileInfo(filePath));
Console.WriteLine($"Excel file created: {filePath}");
}
}catch{
System.Console.WriteLine("Exception : Something went wrong while constructing excel file");
}
}
public static (string ProductName, string Category, int ProductsNbr) GetInfoFromUser(){
// Define the categories array
string[] categories = new string[]
{
"Electronics",
"Clothing-Shoes-Jewelry",
"Home-Kitchen",
"Books",
"Health-Household",
"Toys-Games",
"Beauty-Personal-Care",
"Tools-Home-Improvement",
"Sports-Outdoors",
"Automotive",
"Grocery-Gourmet-Food",
"Pet-Supplies",
"Office-Products",
"Musical-Instruments",
"Industrial-Scientific",
"Baby",
"Arts-Crafts-Sewing",
"Patio-Lawn-Garden",
"Software",
"Movies-TV",
"Video-Games",
"Home-Audio-Theater",
"Computers-Accessories",
"Cell-Phones-Accessories",
"Kindle-Store",
"Digital-Music",
"Appliances",
"Electronics-Accessories-Supplies",
"Luggage-Travel-Gear",
"Watches"
};
// Prompt user for input
Console.WriteLine("Welcome to the Product Information App!");
Console.WriteLine("Please enter the following information:");
// Get ProductName from user
Console.Write("Product Name: ");
string productName = Console.ReadLine() ?? "book";
// Get ProductNumber from user
int productNumber;
while(true){
Console.Write("Product Number: ");
if(int.TryParse(Console.ReadLine(), out productNumber)){
break;
}else{
System.Console.WriteLine("Product Number must be Number !");
}
}
// Get Category from user and validate
string category;
while (true)
{
Console.WriteLine("Available categories:");
foreach (string cat in categories)
{
Console.WriteLine("- " + cat);
}
Console.Write("Category: ");
category = Console.ReadLine() ?? "";
if (Array.IndexOf(categories, category) != -1)
{
break; // Category is valid
}
else
{
Console.WriteLine("Invalid category. Please choose from the provided list.");
}
}
// Display the entered information
Console.WriteLine("\nProduct Information:");
Console.WriteLine($"Product Name: {productName}");
Console.WriteLine($"Product Number: {productNumber}");
Console.WriteLine($"Category: {category}");
return (productName, category, productNumber);
}
}
public record ProductInfo(String? Title, String? Price, String? ImageUrl);
}