Wikipedia is the largest and most-read reference work in history. It was ranked the 7th most popular site in 2022 and hosted by the Wikimedia Foundation, an American non-profit organization funded mainly through donations.
Being one of the best free online sources for information, Wikipedia gave you APIs to fetch the data yourselves. But learning how to use those APIs may take weeks or months to master. In this article, we'll see how to use Golang API wrapper Go-wiki to access and fetch a variety of information from the Wikipedia website.
github.com/trietmn/go-wiki Go-wiki is a Golang Wikipedia API wrapper - The Golang module that makes it easy to access and parse data from Wikipedia. |
---|
Instalation
To install Go-Wiki package, you need to install Go and set your Go workspace first.
- You first need Go installed, then you can use the below Go command to install Go-wiki.
go get -u github.com/trietmn/go-wiki
- Import it in your code.
import "github.com/trietmn/go-wiki"
Examples
1. Getting the summary of any title
Code:
package main
import (
"fmt"
"github.com/trietmn/go-wiki"
)
// Getting summary of Wikipedia page "Rafael Nadal"
func main() {
res, err := gowiki.Summary("Rafael Nadal", 5, -1, false, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Summary: %v\n", res)
}
Output
Summary: Rafael Nadal Parera (Catalan: [rəf(ə)ˈɛl nəˈðal pəˈɾeɾə], Spanish: [rafaˈel naˈðal paˈɾeɾa];
born 3 June 1986) is a Spanish professional tennis player. He is currently ranked world No. 2 in singles by the
Association of Tennis Professionals (ATP). He has been ranked world No. 1 for 209 weeks, and has finished as the year-end No.
2. Searching title and suggestions
Code:
package main
import (
"fmt"
"github.com/trietmn/go-wiki"
)
// Search and get suggestion
func main() {
// If you don't need the suggestion, set the 3rd parameter to false
search_result, suggestion, err := gowiki.Search("nadal", 3, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Search result: %v\n", search_result)
fmt.Printf("Suggestion: %v\n", suggestion)
}
Output:
Search result: [Rafael Nadal Federer–Nadal rivalry Nadal (surname)]
Suggestion: nasal
3. Getting the Wikipedia page information
Code:
package main
import (
"fmt"
"github.com/trietmn/go-wiki"
)
// Getting the Wikipedia page "Rafael Nadal"
func main() {
// Get the page
page, err := gowiki.GetPage("Rafael Nadal", -1, false, true)
if err != nil {
fmt.Println(err)
}
// Get the content of the page
content, err := page.GetContent()
if err != nil {
fmt.Println(err)
}
fmt.Printf("This is the page content: %v\n", content)
}
Output:
This is the page content: Rafael Nadal Parera (Catalan: [rəf(ə)ˈɛl nəˈðal pəˈɾeɾə], Spanish: [rafaˈel naˈðal paˈɾeɾa]; born 3 June 1986)
is a Spanish professional tennis player. He is currently ranked world No. 2 in singles by the Association of Tennis Professionals (ATP).
He has been ranked world No. 1 for 209 weeks, and has finished as the year-end No. 1 five times. Nadal has won an all-time record 22 Grand
Slam men's singles titles, including a record 14 French Open titles. He has won 92 ATP singles titles, including 36 Masters titles, with 63
of these on clay courts. Nadal is one of only two men to complete the Career Golden Slam in singles. His 81 consecutive wins on clay is the
longest single-surface win streak in the Open Era...
Note: The above code will get you the content of the page, if you want to get other information, use other method like:
Methods | Description | Example |
---|---|---|
Equal | Check if 2 pages are equal to each other | page1.Equal(page2) |
GetContent | Get the page content | page.GetContent() |
GetHTML | Get the page HTML | page.GetHTML() |
GetRevisionID | Get revid field of a page | page.GetRevisionID() |
GetParentID | Get parentid field of a page | page.GetParentID() |
GetSummary | Get the summary of the page | page.GetSummary() |
GetImagesURL | Get all of the image URL appear in the page | page.GetImageURL() |
GetCoordinate | Get the page coordinate if exist | page.GetCoordinate() |
GetReference | Get all of the extenal links in the page | page.GetReference() |
GetLink | Get all the titles of Wikipedia page links on a page | page.GetLink() |
GetCategory | Get all of the categories of a page | page.GetCategory() |
GetSectionList | Get all of the sections of the page | page.GetSectionList() |
GetSection | Get the content of a specific section in the page | page.GetSection("History") |
4. Changing the language
Code:
package main
import (
"fmt"
"github.com/trietmn/go-wiki"
)
// Getting summary of Wikipedia page "Rafael Nadal"
func main() {
// Change the Wikipedia language to Vietnamese using the language prefix (ex: vi, fr, en, kr, cn)
gowiki.SetLanguage("vi")
// Get the summary in Vietnamese
res, err := gowiki.Summary("Rafael Nadal", 3, -1, false, true)
if err != nil {
fmt.Println(err)
}
fmt.Printf("Summary: %v\n", res)
}
Output:
Summary: Rafael "Rafa" Nadal Parera (IPA: [rafa'el na'ðal], sinh ngày 3 tháng 6 năm 1986 tại Manacor, Mallorca) là một vận động viên quần vợt
chuyên nghiệp người Tây Ban Nha hiện đang giữ vị trí số 3 thế giới. Nadal được đánh giá là một trong những tay vợt xuất sắc nhất mọi thời đại.
Nadal đang nắm giữ kỷ lục 22 Grand Slam ở nội dung đánh đơn, cùng với đó là 2 huy chương vàng Olympic: đơn nam tại Olympic 2008 và đôi nam tại
Olympic 2016, 36 chức vô địch ATP World Tour Masters 1000, 21 chức vô địch ATP Tour 500, 5 chức vô địch Davis Cup cùng đội tuyển Tây Ban Nha vào
các năm 2004, 2008, 2009, 2011 và 2019 cùng nhiều danh hiệu khác.
5. Other functions that you may use
Functions | Description |
---|---|
Suggest | Get suggestion from your keyword |
Geosearch | Use the Wikipedia Geosearch |
GetRandom | Get some random Wikipedia page titles |
GetAvailableLanguage | Get the list of Wikipedia supported language |
SetMaxCacheMemory | Set the maximum amount of API response stored in the Cache |
SetCacheDuration | Set the maximum "age" of API response stored in the Cache |
Documentation
For further information, check out these sources:
Top comments (0)