Skip to content

Commit e1b4294

Browse files
committed
updated
0 parents  commit e1b4294

File tree

6 files changed

+174
-0
lines changed

6 files changed

+174
-0
lines changed

.github/workflows/go.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
push:
8+
branches: [ "master" ]
9+
pull_request:
10+
branches: [ "master" ]
11+
12+
jobs:
13+
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v3
21+
with:
22+
go-version: 1.18
23+
24+
- name: Build
25+
run: go build ./...
26+
27+
- name: Test
28+
run: go test -v ./...

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# go-cascadia-sample
2+
3+
4+
## Description
5+
6+
Creating [cascadia-sample](https://github.com/yuvan11/cascadia-sample) to understand the css selector library [cascadia](https://github.com/andybalholm/cascadia)
7+
8+
## Run
9+
10+
- cd src
11+
- run `go run sample.go`
12+
13+
## Sample output
14+
15+
List of URLS:
16+
Review url https://www.google.com/search?q=THE+MARINA+MALL&rlz=1C1CHBD_enIN908IN908&oq=th+&aqs=chrome.1.69i57j69i59l2j69i60l5.2415j0j7&sourceid=chrome&ie=UTF-8#lrd=0x3a525a5ed3d3509d:0x51ba8d5c2f099ebb,1,,,
17+
Image URl http://marinamallchennai.com/wp-content/uploads/2020/09/Googlebusiness.png

azure-pipelines.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Go
2+
# Build your Go project.
3+
# Add steps that test, save build artifacts, deploy, and more:
4+
# https://learn.microsoft.com/azure/devops/pipelines/languages/go
5+
# updated
6+
trigger:
7+
- main
8+
- feature/*
9+
10+
pool: ubuntu-latest
11+
12+
variables:
13+
GOPATH: '$(Agent.HomeDirectory)/go' # Go workspace path
14+
GOBIN: '$(GOPATH)/bin' # Go binaries path
15+
GOROOT: '/opt/hostedtoolcache/go/1.15.8/x64' # Go installation path
16+
17+
18+
stages:
19+
- stage: Build
20+
displayName: Build image
21+
22+
jobs:
23+
- job: BuildAndTest
24+
displayName: Build And Test
25+
pool: ubuntu-latest
26+
steps:
27+
- checkout: self
28+
- script: |
29+
export PATH="$(GOROOT)/bin:$(PATH)"
30+
printenv
31+
ls -la
32+
go env
33+
go version
34+
go mod download
35+
go build ./...
36+
go test ./...
37+
workingDirectory: '$(Build.SourcesDirectory)'
38+
displayName: 'Get dependencies, then build and test'

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module go-cascadia-sample
2+
3+
go 1.18
4+
5+
require (
6+
github.com/andybalholm/cascadia v1.3.1
7+
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8
8+
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
2+
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
3+
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8 h1:/6y1LfuqNuQdHAm0jjtPtgRcxIxjVZgm5OTu8/QhZvk=
4+
golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
5+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
7+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
8+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
9+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

sample.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"strings"
7+
8+
"github.com/andybalholm/cascadia"
9+
"golang.org/x/net/html"
10+
)
11+
12+
var feedbackHtml string = `
13+
<section class="feedback-section">
14+
<div class="container">
15+
<div class="row">
16+
<div class="col-md-12 text-center">
17+
<h1 class="single-line">The Customers’ Verdict</h1>
18+
</div>
19+
</div><br>
20+
<div class="row">
21+
<div class="col-md-3">
22+
<div class="feedback-head">
23+
<a href="https://www.google.com/search?q=THE+MARINA+MALL&rlz=1C1CHBD_enIN908IN908&oq=th+&aqs=chrome.1.69i57j69i59l2j69i60l5.2415j0j7&sourceid=chrome&ie=UTF-8#lrd=0x3a525a5ed3d3509d:0x51ba8d5c2f099ebb,1,,," target="_blank">
24+
<img src="http://marinamallchennai.com/wp-content/uploads/2020/09/Googlebusiness.png">
25+
</a>
26+
<p><i class="fa fa-star"></i> <i class="fa fa-star"></i> <i class="fa fa-star"></i> <i class="fa fa-star"></i> <i class="fa fa-star"></i></p><br>
27+
</div>
28+
</div>
29+
30+
</div>
31+
32+
</div>
33+
</section>
34+
`
35+
36+
func Query(n *html.Node, query string) *html.Node {
37+
sel, err := cascadia.Parse(query)
38+
if err != nil {
39+
return &html.Node{}
40+
}
41+
return cascadia.Query(n, sel)
42+
}
43+
44+
func QueryAll(n *html.Node, query string) []*html.Node {
45+
sel, err := cascadia.Parse(query)
46+
if err != nil {
47+
return []*html.Node{}
48+
}
49+
return cascadia.QueryAll(n, sel)
50+
}
51+
52+
func AttrOr(n *html.Node, attrName, or string) string {
53+
for _, a := range n.Attr {
54+
55+
if a.Key == attrName {
56+
return a.Val
57+
}
58+
}
59+
return or
60+
}
61+
62+
// main
63+
func main() {
64+
doc, err := html.Parse(strings.NewReader(feedbackHtml))
65+
if err != nil {
66+
log.Fatal(err)
67+
}
68+
fmt.Printf("List of URLS:\n\n")
69+
for _, p := range QueryAll(doc, "section.feedback-section") {
70+
reviewUrl := AttrOr(Query(p, "div a "), "href", "")
71+
imageUrl := AttrOr(Query(p, "div a img"), "src", "")
72+
fmt.Println("Review url", reviewUrl, "\n", "Image URl", imageUrl)
73+
}
74+
}

0 commit comments

Comments
 (0)