This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
118 lines (89 loc) · 3.79 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using PuppeteerSharp;
using PuppeteerSharp.Input;
namespace CraigslistPuppeteerExperiment
{
class Program
{
static async Task Main(string[] args)
{
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false
});
var page = await browser.NewPageAsync();
await page.GoToAsync("https://mobile.craigslist.org/", new NavigationOptions
{
WaitUntil = new [] {WaitUntilNavigation.Networkidle0}
});
var searchTextbox = await page.QuerySelectorAsync("#query");
await searchTextbox.TypeAsync("nintendo switch");
await searchTextbox.PressAsync(Key.Enter);
await page.WaitForNavigationAsync(new NavigationOptions
{
WaitUntil = new[] { WaitUntilNavigation.Networkidle0 }
});
var searchResults = await page.EvaluateExpressionAsync<List<SearchResult>>(@"
(function () {
let resultNodes = document.querySelectorAll('.rows > *');
let searchResults = [];
for (let i = 0; i < resultNodes.length; i++) {
let node = resultNodes[i];
let shouldStop = node.classList.contains('nearby');
if (shouldStop) {
break;
}
let title = node.querySelector('.result-title').innerText;
let isValid = title.toLowerCase().indexOf('nintendo switch') >= 0;
if (!isValid) {
continue;
}
let date = node.querySelector('.result-date').getAttribute('title');
let amount = node.querySelector('.result-price').innerText;
let locationNode = node.querySelector('.result-hood');
let location = locationNode !== null ? locationNode.innerText : null;
let imgNode = node.querySelector('.result-image img');
let imageSrc = imgNode !== null ? imgNode.getAttribute('src') : null;
let resultUrl = node.querySelector('.result-title').getAttribute('href');
let searchResult = {
title: title,
date: date,
amount: amount,
location: location,
imageSrc: imageSrc,
resultUrl: resultUrl,
newFieldHere: 'Sabotage!'
};
searchResults.push(searchResult);
}
return searchResults;
})();
");
var serializedResults = JsonConvert.SerializeObject(searchResults, Formatting.Indented);
Console.WriteLine(serializedResults);
Console.WriteLine($"Total results: {searchResults.Count}");
var originalWidth = page.Viewport.Width;
await SetViewportWidth(page, 1920);
var screenshotOptions = new ScreenshotOptions
{
FullPage = true
};
await page.ScreenshotAsync("hello-puppeteer-gang.jpeg", screenshotOptions);
await SetViewportWidth(page, originalWidth);
browser.Dispose();
}
private static async Task SetViewportWidth(Page page, int width)
{
await page.SetViewportAsync(new ViewPortOptions
{
Width = width
});
}
}
}