Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Testing
on: [push, pull_request]

jobs:
test:
strategy:
matrix:
go-version: ["1.20", 1.21.x, 1.22.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}

steps:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v4

- name: Test
run: |
go version
go test . -v -cover
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

41 changes: 17 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
htmlquery
====
[![Build Status](https://travis-ci.org/antchfx/htmlquery.svg?branch=master)](https://travis-ci.org/antchfx/htmlquery)
[![Coverage Status](https://coveralls.io/repos/github/antchfx/htmlquery/badge.svg?branch=master)](https://coveralls.io/github/antchfx/htmlquery?branch=master)
# htmlquery

[![Build Status](https://github.com/antchfx/htmlquery/actions/workflows/testing.yml/badge.svg)](https://github.com/antchfx/htmlquery/actions/workflows/testing.yml)
[![GoDoc](https://godoc.org/github.com/antchfx/htmlquery?status.svg)](https://godoc.org/github.com/antchfx/htmlquery)
[![Go Report Card](https://goreportcard.com/badge/github.com/antchfx/htmlquery)](https://goreportcard.com/report/github.com/antchfx/htmlquery)

Overview
====
# Overview

`htmlquery` is an XPath query package for HTML, lets you extract data or evaluate from HTML documents by an XPath expression.

`htmlquery` built-in the query object caching feature based on [LRU](https://godoc.org/github.com/golang/groupcache/lru), this feature will caching the recently used XPATH query string. Enable query caching can avoid re-compile XPath expression each query.
`htmlquery` built-in the query object caching feature based on [LRU](https://godoc.org/github.com/golang/groupcache/lru), this feature will caching the recently used XPATH query string. Enable query caching can avoid re-compile XPath expression each query.

You can visit this page to learn about the supported XPath(1.0/2.0) syntax. https://github.com/antchfx/xpath

XPath query packages for Go
===
# XPath query packages for Go

| Name | Description |
| ------------------------------------------------- | ----------------------------------------- |
| [htmlquery](https://github.com/antchfx/htmlquery) | XPath query package for the HTML document |
| [xmlquery](https://github.com/antchfx/xmlquery) | XPath query package for the XML document |
| [jsonquery](https://github.com/antchfx/jsonquery) | XPath query package for the JSON document |

Installation
====
# Installation

```
go get github.com/antchfx/htmlquery
```

Getting Started
====
# Getting Started

#### Query, returns matched elements or error.

Expand Down Expand Up @@ -70,15 +66,15 @@ list := htmlquery.Find(doc, "//a")
#### Find all A elements that have `href` attribute.

```go
list := htmlquery.Find(doc, "//a[@href]")
list := htmlquery.Find(doc, "//a[@href]")
```

#### Find all A elements with `href` attribute and only return `href` value.

```go
list := htmlquery.Find(doc, "//a/@href")
list := htmlquery.Find(doc, "//a/@href")
for _ , n := range list{
fmt.Println(htmlquery.SelectAttr(n, "href")) // output @href value
fmt.Println(htmlquery.InnerText(n)) // output @href value
}
```

Expand All @@ -89,6 +85,7 @@ a := htmlquery.FindOne(doc, "//a[3]")
```

### Find children element (img) under A `href` and print the source

```go
a := htmlquery.FindOne(doc, "//a")
img := htmlquery.FindOne(a, "//img")
Expand All @@ -103,9 +100,7 @@ v := expr.Evaluate(htmlquery.CreateXPathNavigator(doc)).(float64)
fmt.Printf("total count is %f", v)
```


Quick Starts
===
# Quick Starts

```go
func main() {
Expand All @@ -127,9 +122,7 @@ func main() {
}
```


FAQ
====
# FAQ

#### `Find()` vs `QueryAll()`, which is better?

Expand Down Expand Up @@ -158,6 +151,6 @@ BenchmarkDisableSelectorCache-4 500000 3162 ns/op
htmlquery.DisableSelectorCache = true
```

Questions
===
# Questions

Please let me know if you have any questions.