-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
80 lines (68 loc) · 2.38 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
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
class Program
{
static void Main()
{
var postFolder = @"d:\git\ScrapeRaxxla\Data\Posts\Pages";
if (!Directory.Exists(postFolder))
{
Directory.CreateDirectory(postFolder);
}
// Set up ChromeDriver
using IWebDriver driver = new ChromeDriver();
var pageNumber = GetLastPage(postFolder);
while (true)
{
try {
Console.WriteLine($"Navigation to page {pageNumber}");
driver.Navigate().GoToUrl($"https://forums.frontier.co.uk/threads/the-quest-to-find-raxxla.168253/page-{pageNumber}");
List<string[]> items = [];
IReadOnlyCollection<IWebElement> post = driver.FindElements(By.TagName("article"));
foreach (var item in post)
{
var author = item.GetDomAttribute("data-author");
var id = item.GetDomAttribute("data-content");
if (String.IsNullOrEmpty(id))
{
continue;
}
var folder = new DirectoryInfo(Path.Combine(postFolder, pageNumber.ToString()));
if (!folder.Exists)
{
folder.Create();
}
var fileName = Path.Combine(folder.FullName, id + ".txt");
if (!File.Exists(fileName))
{
File.WriteAllText(fileName, item.Text);
}
}
pageNumber++;
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
break;
}
}
// Close the browser
driver.Quit();
}
private static int GetLastPage(string PagesFolder)
{
var folders = Directory.GetDirectories(PagesFolder);
// Return the last page number, i.e. last foldername
if (folders == null)
return 1;
var lastPage = folders
.Select(ExtractNumberFromText)
.DefaultIfEmpty(0)
.Max();
return lastPage == 0 ? 1 : lastPage;
}
private static int ExtractNumberFromText(string value)
{
var pageNumberPart = new DirectoryInfo(value);
return int.Parse(pageNumberPart.Name);
}
}