-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 1.42 KB
/
main.go
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
package main
import (
"fmt"
"log"
"github.com/playwright-community/playwright-go"
)
func main() {
runOption := &playwright.RunOptions{
SkipInstallBrowsers: true,
}
err := playwright.Install(runOption)
if err != nil {
log.Fatalf("could not install playwright dependencies: %v", err)
}
pw, err := playwright.Run()
if err != nil {
log.Fatalf("could not start playwright: %v", err)
}
chrome := "chrome"
option := playwright.BrowserTypeLaunchOptions{
Channel: &chrome,
}
browser, err := pw.Chromium.Launch(option)
if err != nil {
log.Fatalf("could not launch browser: %v", err)
}
page, err := browser.NewPage()
if err != nil {
log.Fatalf("could not create page: %v", err)
}
if _, err = page.Goto("https://news.ycombinator.com"); err != nil {
log.Fatalf("could not goto: %v", err)
}
entries, err := page.QuerySelectorAll(".athing")
if err != nil {
log.Fatalf("could not get entries: %v", err)
}
for i, entry := range entries {
titleElement, err := entry.QuerySelector("td.title > a")
if err != nil {
log.Fatalf("could not get title element: %v", err)
}
title, err := titleElement.TextContent()
if err != nil {
log.Fatalf("could not get text content: %v", err)
}
fmt.Printf("%d: %s\n", i+1, title)
}
if err = browser.Close(); err != nil {
log.Fatalf("could not close browser: %v", err)
}
if err = pw.Stop(); err != nil {
log.Fatalf("could not stop Playwright: %v", err)
}
}