microdata_mirror/README.md

101 lines
2.4 KiB
Markdown
Raw Normal View History

2013-07-10 19:59:28 +04:00
# microdata
A microdata parser in Go
2012-06-10 21:57:35 +04:00
2012-06-10 22:59:06 +04:00
See [http://www.w3.org/TR/microdata/](http://www.w3.org/TR/microdata/) for more information about Microdata
2013-07-10 19:59:28 +04:00
## Installation
2012-06-10 21:57:35 +04:00
Simply run
go get github.com/iand/microdata
2013-07-10 19:59:28 +04:00
Documentation is at [http://godoc.org/github.com/iand/microdata](http://godoc.org/github.com/iand/microdata)
2012-06-10 21:57:35 +04:00
2013-07-10 19:59:28 +04:00
## Usage
2012-06-10 21:57:35 +04:00
Example of parsing a string containing HTML:
2012-06-10 22:49:15 +04:00
package main
import (
"github.com/iand/microdata"
2012-06-10 21:57:35 +04:00
"net/url"
"strings"
)
2012-06-10 22:49:15 +04:00
func main() {
html := `<div itemscope>
<p>My name is <span itemprop="name">Elizabeth</span>.</p>
</div>`
baseUrl, _ := url.Parse("http://example.com/")
p := microdata.NewParser(strings.NewReader(html), baseUrl)
data, err := p.Parse()
if err != nil {
panic(err)
}
println("Name: ", data.Items[0].Properties["name"][0].(string))
}
Extract microdata from a webpage and print the result as JSON
package main
import (
"bytes"
"github.com/iand/microdata"
"io/ioutil"
"net/http"
"net/url"
"os"
)
func main() {
baseUrl, _ := url.Parse("http://tagger.steve.museum/steve/object/44863?offset=6")
resp, _ := http.Get(baseUrl.String())
defer resp.Body.Close()
html, _ := ioutil.ReadAll(resp.Body)
p := microdata.NewParser(bytes.NewReader(html), baseUrl)
data, _ := p.Parse()
json, _ := data.Json()
os.Stdout.Write(json)
}
2012-06-10 21:57:35 +04:00
2013-07-10 19:59:28 +04:00
## Authors
2012-06-10 21:57:35 +04:00
2013-07-10 19:59:28 +04:00
* [Ian Davis](http://github.com/iand) - <http://iandavis.com/>
## Contributors
## Contributing
* Do submit your changes as a pull request
* Do your best to adhere to the existing coding conventions and idioms.
* Do run `go fmt` on the code before committing
* Do feel free to add yourself to the [`CREDITS`](CREDITS) file and the
corresponding Contributors list in the the [`README.md`](README.md).
Alphabetical order applies.
* Don't touch the [`AUTHORS`](AUTHORS) file. An existing author will add you if
your contributions are significant enough.
* Do note that in order for any non-trivial changes to be merged (as a rule
of thumb, additions larger than about 15 lines of code), an explicit
Public Domain Dedication needs to be on record from you. Please include
a copy of the statement found in the [`WAIVER`](WAIVER) file with your pull request
## License
This is free and unencumbered software released into the public domain. For more
information, see <http://unlicense.org/> or the accompanying [`UNLICENSE`](UNLICENSE) file.